mysql在单个表中多次计数

时间:2013-05-13 10:53:58

标签: mysql sql

我在一个表中有2个查询t_push_member

SELECT
  DATE_FORMAT(`member_subscribe_at`, '%d-%m-%y') Tanggal,
  count(`member_msisdn`) 'total_reg'
FROM
  `t_push_member` 
WHERE
  `service_keycode`='nocan' 
GROUP BY
  Tanggal,
  'total_reg'
ORDER BY
  Tanggal

SELECT
  DATE_FORMAT(`member_unsubscribe_at`, '%d-%m-%Y') Tanggal,
  COUNT(`member_msisdn`) 'total_unreg'
FROM
  `t_push_member` 
WHERE
  `service_keycode`='nocan'
GROUP BY
  Tanggal, 'total_unreg'
ORDER BY
  Tanggal

如何在一个查询中进行2次查询?

2 个答案:

答案 0 :(得分:1)

select * 
from 
( SELECT DATE_FORMAT(member_subscribe_at, '%d-%m-%y') Tanggal, count(member_msisdn),'REG' as Registered FROM t_push_member 
    where service_keycode='nocan' 
    group by Tanggal  

    UNION

    SELECT DATE_FORMAT(member_unsubscribe_at, '%d-%m-%Y') Tanggal, count(member_msisdn),'NOT_REG' as Registered FROM t_push_member 
    where service_keycode='nocan'
    group by Tanggal
)
order by  Tanggal

http://www.w3schools.com/sql/sql_union.asp

了解有关UNION的更多信息

答案 1 :(得分:0)

在两者之间使用“联合” - 而不是和