I am sorry for disturbing, but I need little help from you professionals...
I have two tables:
Table1:
ID, date, menu1, menu2, menu3, menu4
and
Table2:
ID, name, somethingElse, somethingElse2
In Table1 (menu1, menu2, menu3, menu4) are IDs of Table2. What I need is query "date" value to database and get four rows from Table2 (depending on menuX values of Table1) Any suggestions, please?
Read the MySQL-docs and look in a MySQL-Tutorial on JOINing. There you will find something like this:
Do a simple SELECT
spiced up with a JOIN
and tell MySQL to use ID
as join-colum:
SELECT Table1.`date` FROM Table1
JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table1.menu1 = "someValue"
AND Table1.menu2 = "someOtherValue"
If this runs a bit slow try adding an INDEX
on one of the tables id-col:
ALTER TABLE Table1 ADD INDEX myIndex (ID)
You can check how many rows are examined by stating
EXPLAIN SELECT Table1.`date` FROM Table1 ...
Ok, now I understand your question. Thanks to your provided examples here is a query that should solve your Problem. The trick here is to use a UNION
to append one query result under another. Doing this four times will do the trick:
SELECT b.* FROM dailymenu as a JOIN menu as b ON a.menu1 = b.ID WHERE a.`date` = '2014-10-08'
UNION
SELECT b.* FROM dailymenu as a JOIN menu as b ON a.menu2 = b.ID WHERE a.`date` = '2014-10-08'
UNION
SELECT b.* FROM dailymenu as a JOIN menu as b ON a.menu3 = b.ID WHERE a.`date` = '2014-10-08'
UNION
SELECT b.* FROM dailymenu as a JOIN menu as b ON a.menu4 = b.ID WHERE a.`date` = '2014-10-08'
Your menu sounds very tasty :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.