1
lichao 2022-05-13 09:41:19 +08:00
GROUP BY 1 ?? 你这么写,用意是什么?
|
2
gogogo1203 OP @lichao 不然怎么用 count? 这里的 by 1 或者 by t.tutorial_id 都是一样的。你是有更好的方法?
|
3
gogogo1203 OP 找到解决方案了.
const q = ` SELECT a.*, u.image FROM ( SELECT t.tutorial_id, t.username, t.version, t.date_created, t.date_updated, t.tag_name, t.slug, t.title,t.description, t.content, t.published, t.deleted, t.date_deleted, CASE WHEN COUNT(c.tutorial_id) > 0 THEN TRUE ELSE FALSE END AS collected, CASE WHEN COUNT(l.tutorial_id) > 0 THEN TRUE ELSE FALSE END AS liked, c.user_id, COUNT(l.tutorial_id) as likecount FROM collections c JOIN tutorials t ON t.tutorial_id = c.tutorial_id LEFT JOIN likes l on c.tutorial_id = l.tutorial_id AND l.user_id = c.user_id LEFT JOIN likes lc ON c.tutorial_id = lc.tutorial_id WHERE c.user_id = :user_id GROUP BY c.user_id, t.tutorial_id ) a JOIN users u USING(user_id) WHERE a.user_id = :user_id AND a.deleted = FALSE ` |