user3767986
user3767986

Reputation: 25

How to get all the rows that have multiple values

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

I want to have last records of services which have serviceType of 1 and 2, when I use this script with IN query, I get 2 rows of services which has serviceType of 1, but I want to have two records, which have 1 and 2 as serviceTypes, sorry for my english

Upvotes: 1

Views: 54

Answers (3)

FuzzyTree
FuzzyTree

Reputation: 32392

(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)

Upvotes: 2

Sathish
Sathish

Reputation: 4487

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

Upvotes: 0

ah_hau
ah_hau

Reputation: 768

can you try union ?

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

Upvotes: 0

Related Questions