Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:07:20+00:00 2026-05-28T15:07:20+00:00

I am very new in MySQL & PHP. I have 2 tables like these

  • 0

I am very new in MySQL & PHP.
I have 2 tables like these with more than thousands of row.

Table "Company A" 
id | Date       | Prize_1   | Prize_2   | Prize_3 | Prize_Consolation
1  | 2011-01-13 | "car"     | "ipad2"   | "bag"   | "mouse" 
2  | 2011-02-13 | "ps3"     | "iphone4" | "mouse" | "bag"

Table "Company B" 
id | Date       | Prize_1   | Prize_2   | Prize_3 | Prize_Consolation 
1  | 2011-01-18 | "tv"      | "iphone4" | "ipod"  | "bag" 
2  | 2011-02-13 | "iphone4" | "ipad"    | "bag"   | "mouse"

User will randomly send query to search history information on a ‘prize’ when and which company was issued.

Example: I would like to do a query search on both tables for the prize “iphone4” and it should return results something like this:

Company   | Date       | Prize
Company A | 2011-02-13 | Prize_2
Company B | 2011-01-18 | Prize_2
Company B | 2011-02-13 | Prize_1

Total 3 prizes for "iphone4"

What MySQL query would achieve this lookup search?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:07:21+00:00Added an answer on May 28, 2026 at 3:07 pm

    These really should not be two separate tables, but one table with a company identifying column.

    However your situation can be solved in a hackish way:

    SELECT
      'Company A' AS Company,
      Date, 
      CASE 
        WHEN Prize_1 = 'iphone4' THEN 'Prize_1'    
        WHEN Prize_2 = 'iphone4' THEN 'Prize_2'
        WHEN Prize_3 = 'iphone4' THEN 'Prize_3'
        WHEN Prize_Consolation = 'iphone4' THEN 'Prize_Consolation'
        ELSE NULL
      END AS Prize
    FROM `Company A`
    WHERE 
      Prize_1 = 'iphone4'
      OR Prize_2 = 'iphone4'
      OR Prize_3 = 'iphone4'
      OR Prize_Consolation = 'iphone4'
    /* UNION performs the same query against both tables and combine the results */
    UNION
    SELECT 
      'Company B' AS Company,
      Date, 
      CASE 
        WHEN Prize_1 = 'iphone4' THEN 'Prize_1'    
        WHEN Prize_2 = 'iphone4' THEN 'Prize_2'
        WHEN Prize_3 = 'iphone4' THEN 'Prize_3'
        WHEN Prize_Consolation = 'iphone4' THEN 'Prize_Consolation'
        ELSE NULL
      END AS Prize
    

    FROM Company B
        WHERE 
          Prize_1 = ‘iphone4’
          OR Prize_2 = ‘iphone4’
          OR Prize_3 = ‘iphone4’
          OR Prize_Consolation = ‘iphone4’

    The more appropriate table layout for this design would look like:

    Table Prizes
    ---------------
    id INT AUTO_INCREMENT,
    Date DATETIME,
    Company VARCHAR(256),
    Prize_Type INT,
    Prize_Item VARCHAR(256)
    
    Table Prize_Types
    ---------------
    Prize_Type INT AUTO_INCREMENT PRIMARY KEY,
    Prize_Description VARCHAR(32)
    

    Prizes

    1 | 2012-01-28 08:00:00 | 'Company A' | 3 | 'some prize'
    2 | 2012-01-28 08:00:00 | 'Company A' | 2 | 'ipad'
    3 | 2012-01-28 08:00:00 | 'Company A' | 1 | 'junky phone'
    4 | 2012-01-28 08:00:00 | 'Company b' | 4 | 'iphone4'
    

    Prize_Types

    1 | 'Prize 1'
    2 | 'Prize 2'
    3 | 'Prize 3'
    4 | 'Consolation Prize'
    

    Query this as:

    SELECT
      Company,
      Date,
      Prize_Item,
      Prize_Description
    FROM Prizes JOIN Prize_Types ON Prize_Types.Prize_Type = Prizes.Prize_Type
    WHERE Prize_Item = 'iphone4'
    

    In fact, you might also want the companies and prize items normalized out into their own tables as well like the Prize_Types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to MySQL and I have a stored procedure that I'd like
I am very new to PHP and MySQL and I have some questions. First,
I am very, very new to MYSQL.I tried to create a table named option.
I am VERY new to mySQL, please bear with me. I have an array
I am very new to mysql and php. I need to put an array
I'm very new to php/MySQL and I'm having a bit of trouble. Help would
I'm very new to MySQL so I need some help please, I have a
I am very new to MySQL joins and I have put this together, it
I very new to Python, and fairly new to regex. (I have no Perl
I still very new using Subversion. Is it possible to have a working copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.