开发者

Select, where id = multiple from other select

开发者 https://www.devze.com 2022-12-18 07:05 出处:网络
I\'m tired and I can\'t wrap my head around this. I have two tables: table menu @id = id, @nr = id of parent

I'm tired and I can't wrap my head around this. I have two tables:

table menu
@id = id, @nr = id of parent
|======|======|  
|  id  |  nr  |
|======|======|
|  10  |   9  |
|  11开发者_StackOverflow社区  |  10  |
|  12  |  10  |
|  13  |  10  |
|======|======|

table content
@id = id, @mf = menu - first in tree, @mi = menu item linked to content
|======|======|======|
|  id  |  mf  |  mi  |
|======|======|======|
|  85  |   9  |  11  |
|  89  |   9  |  12  |
|  95  |   9  |  13  |
|======|======|======|

Now, I need to select all rows from content, where (mi = menu.id), but i need to select menu.id from menu first by selecting * from menu where nr = 10 (or any number, which I put to my sql with PHP)

I really can't wrap my head around all those UNIONS, LEFT JOINS, HAVING and stuff...

Thank you

Edit: Thank you ALL, shame I can pick just one good :)


SELECT C.* FROM content C
INNER JOIN menu ON C.mi = menu.id AND menu.nr = 10;

UPDATE:

In response to your comment, you might find the W3 Schools section on SQL JOINS a helpful resource to get started with.


Is this what you're asking for? The following will return all rows in content where mi is in menu.id and nr=10.

select *
from content
where mi in (select id from menu where nr=10)


SELECT * FROM content WHERE mi IN(SELECT id FROM menu WHERE nr = 10)


untested

SELECT content.* FROM content, menu WHERE menu.nr = 10 AND content.mi = menu.id

or

SELECT c.*, menu.nr FROM menu LEFT JOIN content AS c ON menu.id = c.mi WHERE menu.nr = 10

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号