【问题标题】:Counting and joining two tables计算和连接两个表
【发布时间】:2010-04-23 12:33:07
【问题描述】:

Eventhosts – 包含三个常规主持人和一个“其他”字段(如果有人替换他们)或编辑:作为嘉宾主持人(除了常客)

eventid | host (SET[Steve,Tim,Brian,other])
-------------------------------------------
      1 | Steve
      2 | Tim
      3 | Brian
      4 | Brian
      5 | other
      6 | other

活动

id | other | name etc.
----------------------
1  |       | …
2  |       | …
3  |       | …
4  | Billy | …
5  | Billy | …
6  | Irwin | …

这个查询:

SELECT h.host, COUNT(*) AS hostcount
FROM host AS h
LEFT OUTER JOIN event AS e ON h.eventid = e.id
GROUP BY h.host

返回:

Steve | 1
Tim   | 1
Brian | 2
other | 2

我希望它返回:

Steve | 1
Tim   | 1
Brian | 2
Billy | 1
Irwin | 1

或者:

Steve |       | 1
Tim   |       | 1
Brian |       | 2
other | Billy | 1
other | Irwin | 1

不是

Steve |       | 1
Tim   |       | 1
Brian |       | 1
Brian | Billy | 1
other | Billy | 1
other | Irwin | 1

有人可以告诉我如何实现这一目标或为我指明方向吗?

【问题讨论】:

    标签: mysql join count


    【解决方案1】:

    使用这个:

    SELECT IF(h.host != 'other', h.host, e.other) as the_host, COUNT(e.*) AS hostcount
    FROM host h
    LEFT JOIN event e ON h.eventid = e.id
    GROUP BY the_host
    

    请注意,不要使用COUNT(*),如果主持人没有活动,它将显示1而不是0。使用COUNT(e.*)

    对于最后的结果,使用这个:

    SELECT h.host, e.other, COUNT(e.*) AS hostcount
    FROM host h
    LEFT JOIN event e ON h.eventid = e.id
    GROUP BY IF(h.host != 'other', h.host, e.other),
       h.host, e.other -- try repeating it here
    

    [编辑]

    尝试了以下查询(即没有建议重复 GROUP BY 上的字段),它也适用于您的原始问题和您编辑的问题。我现在才安装 MySQL,不知道是不是跟数据库类型有关,我只启用了 InnoDB 和严格的设置。顺便说一句,COUNT(e.*)(我认为这是 ANSI SQL 接受的)在 MySQL 上不起作用,而必须使用COUNT(e.id)(或者您可能已经在查询中进行了修改):

    SELECT h.host, e.other, COUNT(e.id) AS hostcount
    FROM host h
    LEFT JOIN event e ON h.eventid = e.id
    GROUP BY IF(h.host != 'other', h.host, e.other)
       -- note: i removed the suggested repeating of fields here, it works on my box here.     
       -- Settings: InnoDB and strict mode
    

    【讨论】:

    • 谢谢,但这只会返回第一个“其他”主机,而不是第二个。
    • 嗯.. 它不返回欧文?有趣的。我的查询的输出是什么?
    • 好吧,我的示例是我的数据库的简化版本,几乎完全相同。并翻译它返回:史蒂夫,11;蒂姆,11 岁;布赖恩,5 岁;比利,2 岁。(比利应该有 1 个,欧文也应该有一个。)
    • @Eikern:尝试在 GROUP BY 中重复 h.host 和 e.other
    【解决方案2】:
    SELECT eh.host, e.other, count(*)
    FROM Eventhosts eh
    LEFT JOIN Event e ON (eh.eventid = e.id)
    GROUP BY eh.host, e.other
    

    返回

    Steve |       | 1
    Tim   |       | 1
    Brian |       | 1
    other | Billy | 1
    other | Irwin | 1
    

    【讨论】:

      【解决方案3】:

      只需删除GROUP BY(因为您不希望它折叠该列的值)并将event.other 列添加到列列表中。

      SELECT h.host, e.other, COUNT(*) AS hostcount
          FROM host AS h
          LEFT OUTER JOIN event AS e ON h.eventid = e.id
      

      我只记得您也可以通过以下方式实现第一个解决方案:

      SELECT IF(h.host = 'other', e.other, h.host) AS host, COUNT(*) AS hostcount
          FROM host AS h
          LEFT OUTER JOIN event AS e ON h.eventid = e.id
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-15
        • 2021-10-13
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        相关资源
        最近更新 更多