SQL查询中的多个和

时间:2013-04-30 09:53:53

标签: mysql sum

SELECT sum( plot_status = 'OPEN' ) AS OPEN 
     , sum( plot_status = 'SOLD' ) AS SOLD
FROM `tbl_plot`
GROUP BY `plot_status

这是给予

OPEN   SOLD
7       0
0       8

如何制作

OPEN  SOLD
7      8

或者可能吗?

3 个答案:

答案 0 :(得分:2)

只需删除GROUP BY子句即可。

SELECT sum( plot_status = 'OPEN' ) AS `OPEN` ,
       sum( plot_status = 'SOLD' ) AS SOLD
FROM  `tbl_plot`

答案 1 :(得分:1)

如果存在plot_name或id,则按其分组,而不是plot_status:

SELECT sum( plot_status = 'OPEN' ) AS
OPEN , sum( plot_status = 'SOLD' ) AS SOLD
FROM `tbl_plot`
GROUP BY //`plot_name or plot_id

这对你个人情节有用。 如果您不想这样,那么删除group by子句。

答案 2 :(得分:1)

选择*来自 ( 选择总和(plot_status ='OPEN'FROM tbl_plot)AS OPEN 选择总和(plot_status ='已售出来自tbl_plot)已售出 )TBL