简体   繁体   中英

how to combine result of two queries in one resultset

I have a table named 'rating' it has two fields 'cab_id' and 'rating' which stores id for cabs and its rating as either 1 or 0. I want to retrieve values as up or down as sum of counts as 1 or 0. I used two queries as

      (SELECT count(rating) as up FROM 

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

and

     (SELECT count(rating) as down FROM 

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

I want to retrieve in the format of one result .

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

         ----------


       1             2         


        ----------

Please lend a help here !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM