请教 psql 多个表 Join+ count 的问题 - V2EX
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 1964 次点击
    这是一个创建于 1278 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我 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     Solana     1076 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 23:11 PVG 07:11 LAX 15:11 JFK 18:11
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86