Problem with LIMIT & IN/ALL/ANY/SOME subquery

后端 未结 4 1667
予麋鹿
予麋鹿 2020-12-06 01:50

I have this query:

SELECT count(cp.CxID) as intSmokers 
FROM CustPrimarySmoking cp 
JOIN Customer c ON cp.CxID = c.CustomerID 
WHERE 
cp.CxID IN (SELECT CxID         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 02:18

    You don't need to use the subquery to retrieve all the records, just exclude the first one:

    SELECT count(cp.CxID) as intSmokers FROM CustPrimarySmoking cp JOIN Customer c ON cp.CxID = c.CustomerID WHERE cp.CxID > (SELECT cxID FROM CustPrimarySmoking ORDER BY cxID LIMIT 1)

    Assuming that cxid is numeric

提交回复
热议问题