【问题标题】:How to create multiple counts in a mysql statement如何在 mysql 语句中创建多个计数
【发布时间】:2013-04-14 12:01:48
【问题描述】:

我在 mysql 语句中计算超过 2 个计数时遇到了一些麻烦。

我的count(b.entry_id) as totalbooking 不起作用。我做错了什么?语句设置也正确吗?

我就是这样尝试的:

"SELECT 
    t.restaurant_id as restaurant_id, ct.title as title, 
    count(DISTINCT t.cardid) as totalmembers, 
    count(t.restaurant_id) as totaltransactions, 
    count(b.entry_id) as totalbooking
    from transactions as t   
    inner join exp_menucard_booking as b on (t.restaurant_id = b.entry_id)
    inner join exp_channel_titles as ct on (t.restaurant_id = ct.entry_id)
    inner JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id)
    where t.cardid != 88888888 and ct.status = 'open'
    group by t.restaurant_id
    order by ct.title asc";

【问题讨论】:

  • 不工作是什么意思?
  • count(b.entry_id) 因为 totalbooking 不计算在内。其他一切正常。

标签: mysql join count


【解决方案1】:

使用此模式计算总行集的子集:

sum( case when ColumnToBeTested = trueCondition then 1 else 0 end) as SubCount

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2019-07-03
    • 2014-07-27
    • 1970-01-01
    • 2017-06-17
    相关资源
    最近更新 更多