如何在一个结果集中组合两个查询的结果

时间:2013-08-09 05:55:30

标签: mysql

我有一张名为'rating'的表 它有两个字段'cab_id'和'rating',它存储cab的id及其等级为1或0。 我想将值作为向上或向下检索为计数总和为1或0。

我使用了两个查询
      (SELECT count(rating) as up FROM 

     `rating` WHERE cab_id=101 and rating=1 )

     (SELECT count(rating) as down FROM 

     `rating` WHERE cab_id=101 and rating=0 )

我想以一个结果的格式检索。

         ----------
      up          down

         ----------


       1             2         


        ----------

请在这里帮忙!

1 个答案:

答案 0 :(得分:2)

SELECT
    SUM(IF(rating=1,1,0)) up,
    SUM(IF(rating=0,1,0)) down
FROM rating
WHERE cab_id=101