MySQL How to Return Unique/Distinct Results?

前端 未结 3 542
清歌不尽
清歌不尽 2021-01-14 13:06

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 c         


        
3条回答
  •  不知归路
    2021-01-14 13:36

    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)
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题