如何组合这两个select语句

时间:2010-10-01 23:06:32

标签: mysql

我想结合这两个陈述,但似乎无法做到正确。

我需要3列。一个用于总计,月份和损失。

这是我到目前为止所拥有的

SELECT Count(patient1.patient_id) AS total FROM patient AS total;
SELECT Count(patient2.patient_id) AS losses, patient2.mo AS `month`
FROM patient AS patient2
WHERE patient1.rx_exp BETWEEN '2010-10-01' AND '2010-11-01';

编辑我需要在一行中包含所有三列。

1 个答案:

答案 0 :(得分:1)

SELECT 
    Count(patient_id) AS total,
    Count(case when rx_exp >= '2010-10-01' 
               AND rx_exp < '2010-11-01' then 1 end) AS losses,
    'October' AS `month`
FROM patient;

你可以在这个月使用它,但可能没有意义。

MAX(case when rx_exp >= '2010-10-01' 
     AND rx_exp < '2010-11-01' then patient2.mo end) AS `month`