假设你有3张桌子..
articles(id,id_cat)
categories(id)
messages(id,id_cat,id_article)
..其中articles
与categories
和messages
相关,可以与articles
或categories
相关联(用户可以在类别或文章)
在假设的当前类别id=55
中,我可以执行哪个查询来列出当前类别和属于当前类别的文章中发布的所有邮件?
- UNION SOLUTION -
Poodlehat建议使用UNION,对于这个特殊问题将是:
SELECT * FROM (
(SELECT m.id FROM messages AS m INNER JOIN categories AS c ON c.id=m.id_cat
WHERE m.id_cat=55)
UNION
(SELECT m.id FROM messages AS m INNER JOIN articles AS a ON a.id=m.id_article
WHERE a.id_cat=55)
) AS foo
----
现在,有没有办法这样做没有UNION ?
答案 0 :(得分:0)
SELECT m.id
FROM messages m
WHERE m.id_cat=55
union
SELECT m.id
FROM messages m, articles a
WHERE a.id_cat=55
AND m.id_article=a.id
没有工会就这样做,嗯,现在你正在挑战我......看看这是否有效
SELECT m.id
FROM messages m LEFT OUTER JOIN articles a ON m.id_article=a.id
WHERE m.id_cat=55 OR a.id_cat=55
答案 1 :(得分:-1)
试试这个::
Select * from
messages m
inner join categories c on (m.id_cat=c.id)
inner join article a on (c.id=a.id_cat)
where m.id_cat=55