三个表join,并且第一个表有查询条件,正确语句:
SELECT * FROM (SELECT b.*,u.username, bt.type, COUNT(c.id) as comments_count
FROM blogs AS b
INNER JOIN users AS u ON b.user_id = u.id
INNER JOIN blog_types as bt ON b.blog_type_id = bt.id
LEFT JOIN comments AS c ON b.id = c.blog_id
GROUP BY b.id) as r
WHERE r.is_private = FALSE AND r.deleted_at IS NULL
ORDER BY click_count DESC
where语句中的两个条件其实是blogs中的字段,但我用blogs作为查询条件字段的表名时就报错,错误语句:
SELECT b.*,u.username, bt.type, COUNT(c.id) as comments_count
FROM blogs AS b
INNER JOIN users AS u ON b.user_id = u.id
INNER JOIN blog_types as bt ON b.blog_type_id = bt.id
LEFT JOIN comments AS c ON b.id = c.blog_id
GROUP BY b.id
WHERE b.is_private = FALSE AND b.deleted_at IS NULL
错误:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE b.is_private = FALSE AND b.deleted_at IS NULL' at line 7
如何使用gorm来表示上述语句?
|