【问题标题】:Count Column in multiple inner join多个内部连接中的计数列
【发布时间】:2022-01-10 00:51:06
【问题描述】:

我想获得 groupby (bebleiotheque.type) 的计数,它的工作,但是当我添加多个 many2many 字段来为每个 bebeliotheque 行获取多本书或多个雇主时,我的计数会增加

如何获得 groupby 的真实计数(bebeliotheque 类型)

Example on db<>fiddle

查看我的应用列表 bebeliotheque,我重新组合了所有相同的 bebeliotheque 名称,但我的(计数)不正确

预览

图片>http://rezigdev.com/image_202112522328.jpg

视频 > https://rezigdev.com/preview.mp4

sql查询

SELECT 
        bibliotheque.name, GROUP_CONCAT(book.name)  as books,  GROUP_CONCAT(employers.name)  as employers, COUNT(bibliotheque.name)
FROM 
        bibliotheque
        left join 
          x_x_bibliotheque_book 
          ON x_x_bibliotheque_book.bibliotheque_id = bibliotheque.id
        left join 
          book 
          ON x_x_bibliotheque_book.book_id = book.id
          
        left join 
          x_x_bibliotheque_employers 
          ON x_x_bibliotheque_employers.bibliotheque_id = bibliotheque.id
        left join 
          employers 
          ON x_x_bibliotheque_employers.employer_id = employers.id
          
          
Group BY 
        bibliotheque.name

【问题讨论】:

  • 再次,用简单的英语尝试解释你的逻辑是什么,你想数什么?解释一下
  • 我不确定我是否理解您想要的,但我尝试:如果您将语句中的 COUNT(...) 替换为 COUNT(distinct book.name),这会带来预期的结果吗?因为你想计算每个围兜里的书本数量?
  • 请将提问所需的所有内容放在您的帖子中,而不仅仅是在链接中。请通过编辑而不是 cmets 进行澄清。请使用标准拼写和标点符号。请use text, not images/links, for text--including tables & ERDs。转述或引用其他文本。只提供您需要的东西并将其与您的问题联系起来。仅将图像用于无法表达为文本或增强文本的内容。在图像中包含图例/键和说明。对于代码问题,请提供minimal reproducible example

标签: mysql sql count inner-join


【解决方案1】:

解决方案: COUNT(distinct bibliotheque.id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多