V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
gogogo1203
V2EX  ›  MySQL

请教 psql 多个表 Join+ count 的问题

  •  
  •   gogogo1203 · 2022-05-12 16:58:51 +08:00 · 1521 次点击
    这是一个创建于 952 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我 google 了快 1 个小时了,实在找不出答案。 谢谢了。

    SELECT t.*,
    tt.tag_name,
    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,
    COUNT(lc.tutorial_id) as likecount
    FROM
    tutorials t
    LEFT JOIN
    collections c ON t.tutorial_id = c.tutorial_id AND c.user_id = 'ec02a580-f9cd-4941-94c4-13a0c86246e3'
    LEFT JOIN
    likes l ON t.tutorial_id = l.tutorial_id AND l.user_id = 'ec02a580-f9cd-4941-94c4-13a0c86246e3'
    LEFT JOIN
    likes lc ON t.tutorial_id = lc.tutorial_id
    LEFT JOIN
    tutorials_tags tt on t.tutorial_id = tt.tutorial_id
    WHERE
    t.tutorial_id = '78e76b5b-9699-47a4-9c41-2c2f1cfb4059'
    GROUP BY 1


    ERROR: column "tt.tag_name" must appear in the GROUP BY clause or be used in an aggregate function

    query 从表 like 里数这个 tutorial_id 有多少 like 和用户是否喜欢, 从 collections 查用户是否已经收藏, 从 tag 里查 tag_name. 我卡在 tag_name 不能 group by.
    麻烦了!
    3 条回复    2022-05-18 23:29:09 +08:00
    lichao
        1
    lichao  
       2022-05-13 09:41:19 +08:00
    GROUP BY 1 ?? 你这么写,用意是什么?
    gogogo1203
        2
    gogogo1203  
    OP
       2022-05-13 19:33:17 +08:00
    @lichao 不然怎么用 count? 这里的 by 1 或者 by t.tutorial_id 都是一样的。你是有更好的方法?
    gogogo1203
        3
    gogogo1203  
    OP
       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
    `
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2909 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 14:44 · PVG 22:44 · LAX 06:44 · JFK 09:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.