MySQL How to Return Unique/Distinct Results?

前端 未结 3 538
清歌不尽
清歌不尽 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:31

    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
    
    0 讨论(0)
  • 2021-01-14 13:33

    You could try SELECT DISTINCT instead of SELECT

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题