MySQL How to Return Unique/Distinct Results?

折月煮酒 提交于 2019-12-01 09:20:11

You could try SELECT DISTINCT instead of SELECT

cristian

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