我有一张名为'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
----------
请在这里帮忙!
答案 0 :(得分:2)
SELECT
SUM(IF(rating=1,1,0)) up,
SUM(IF(rating=0,1,0)) down
FROM rating
WHERE cab_id=101