开发者

MySQL How to Return Unique/Distinct Results?

I'm running the following MySQL query to find cars that don't have manuals (and ha开发者_JAVA技巧ve black wheels, etc.)

SELECT `cars`.* FROM `cars`
INNER JOIN wheels ON cars.id = wheels.car_id
LEFT OUTER JOIN manuals ON cars.id = manuals.car_id
WHERE (cars.created_at > '2010-09-09'
AND wheels.color = 'Black'
AND wheels.created_at < '2011-01-05'
AND manuals.car_id IS NULL)

The results of the query look correct, but it returns the car with id 27 twice. How do I change the query so that all results are unique (no duplicates)?


You could try SELECT DISTINCT instead of SELECT


add group by cars.id at the end of the query EG:

SELECT `cars`.* FROM `cars`
INNER JOIN wheels ON cars.id = wheels.car_id
LEFT OUTER JOIN manuals ON cars.id = manuals.car_id
WHERE (cars.created_at > '2010-09-09'
    AND wheels.color = 'Black'
    AND wheels.created_at < '2011-01-05'
    AND manuals.car_id IS NULL)
GROUP BY cars.id


Presuming that cars.id is a unique primary key, one of those joins is causing the Cartesian product. That is to say: either wheels or manuals contain more than one match for cars.id = 27.

Subqueries are often a good tool for eliminating Cartesian products. The example query below shows two methods of using subqueries.

  1. The first subquery ensures that we're only looking at cars with black wheels where that record was created before 01/05/2011. The GROUP BY clause ensures that we only return one record per w.car_id.

  2. The second subquery (sometimes called a correlated subquery) ensures that there is no manual found for each car in the main query.

Not tested, but conveys the idea:

SELECT `cars`.* 
  FROM `cars`
       JOIN (
           SELECT w.car_id
             FROM wheels w
            WHERE w.color = 'Black'
              AND w.created_at < '2011-01-05'
         GROUP BY w.car_id
       ) wheels 
       ON cars.id = wheels.car_id
WHERE 
    cars.created_at > '2010-09-09'
AND
    NOT EXISTS (SELECT m.car_id FROM manuals m WHERE m.car_id = cars.id)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜