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?
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:
FROM
Company BWHERE
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:
Prizes
Prize_Types
Query this as:
In fact, you might also want the companies and prize items normalized out into their own tables as well like the Prize_Types.