gogogo1203
2022-05-18 23:29:09 +08:00
找到解决方案了.
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
`