1

查询1:

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'

查询2:

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

查询3:

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'

本质上,我不确定正确加入这些的语法。我编写了一个简单地连接它们的查询,但如果论坛缓存表包含未经批准的论坛 id,它根本不会返回整行。

我真正需要的是 query1 和 query2 在类别 id 上左连接,查询 3 在 id = catID 上左外连接。

4

1 回答 1

0

查询1:

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'

查询2:

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

查询3:

不太确定您对此有何要求,但试了一下:

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'
于 2010-04-08T16:40:12.960 回答