MySQL How to Return Unique/Distinct Results?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 06:31:56

问题


I'm running the following MySQL query to find cars that don't have manuals (and have 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)?


回答1:


You could try SELECT DISTINCT instead of SELECT




回答2:


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



回答3:


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)


来源:https://stackoverflow.com/questions/4663181/mysql-how-to-return-unique-distinct-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!