开发者

How do I join these queries?

开发者 https://www.devze.com 2022-12-25 22:09 出处:网络
query1: SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_rep

query1:

SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_report AS report_id FROM category, reports_category_layout WHERE category.id = reports_category_layout.catID AND reports_category_layout.site_code = 'las'

query2:

SELECT DISTINCT category.id, COUNT(forum.id) AS posts, SUM(forum.view) AS views FROM category, forum WHERE category.id = forum.catID AND forum.approved = 'yes' AND forum.site_code = 'las' GROUP BY category.id

query3:

SELECT forum.catID, forum.title, forum.paragraph, forum.created, users.alias, forum.userID FROM forum, users, forum_cache WHERE forum.catID = forum_cache.catID AND forum.id = forum_cache.last_report AND users.id = forum.userID AND forum.approved = 'yes'

Essentially, I am unsure about the syntax to join these properly. I have written a query that simply joins them, but in the case that the forum cache table contains an unapproved forum id, it will simply no开发者_运维知识库t return the entire row.

what I really need is for query1 and query2 to be left joined on the category id, and for query 3 to be left outer joined on id = catID.


query1:

SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_report AS report_id 
FROM category c
LEFT OUTER JOIN reports_category_layout rcl on c.id = rcl.catID
    AND rcl.site_code = 'las'

query2:

SELECT DISTINCT category.id, COUNT(forum.id) AS posts, SUM(forum.view) AS views 
FROM category c
LEFT OUTER JOIN forum f on c.id = f.catID 
    AND f.approved = 'yes' 
    AND f.site_code = 'las' 
GROUP BY c.id

query3:

Not really sure what you are asking for on this, but took a stab at it:

SELECT forum.catID, forum.title, forum.paragraph, forum.created, users.alias, forum.userID 
FROM forum f
INNER JOIN users u on u.id = f.userID
LEFT OUTER JOIN forum_cache fc on f.catID = fc.catID 
    and f.id = fc.last_report 
WHERE f.approved = 'yes'
0

精彩评论

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