如何获取具有多个值的所有行

时间:2014-07-18 09:20:43

标签: mysql sql

SELECT *
FROM Service
WHERE cusId='$cusId'
  AND serviceType IN (1,2)
ORDER BY date DESC LIMIT 2

我想拥有serviceType为1和2的服务的最后记录,当我在IN查询中使用此脚本时,我获得了2行服务,其serviceType为1,但我希望有两条记录, 1和2作为serviceTypes,对不起我的英文

3 个答案:

答案 0 :(得分:2)

(select * from Service 
where cusId='$cusId' 
and serviceType = 1 
order by date desc limit 1)

UNION

(select * from Service 
where cusId='$cusId' 
and serviceType = 2 
order by date desc limit 1)

答案 1 :(得分:0)

你可以尝试结合吗?

SELECT top 1 * FROM Service where cusId='$cusId' and serviceType = 1 order by date DESC
UNION
SELECT top 1 * FROM Service where cusId='$cusId' and serviceType = 2 order by date DESC

答案 2 :(得分:0)

SELECT id,MAX(date) FROM Service 
where cusId='$cusId' 
and serviceType = 1 
 limit 1

UNION All

SELECT id,MAX(date) FROM Service 
where cusId='$cusId' 
and serviceType = 2 
limit 1