SQL : find rows and sort according to number of matching columns?

后端 未结 4 1389
星月不相逢
星月不相逢 2020-12-19 23:11

Let\'s imagine that we are having \"cars\" table with such a simple structure...

car_id INT
color ENUM(\'black\',\'white\',\'blue\')
weight ENUM(\'light\',\'         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 23:47

    There are probably a few ways to optimise the sub-queries, but without using case statements or sub-optimal join clauses:

    select
            *
        from
            (
                select
                        selection.CarId,
                        selection.Colour,
                        selection.Weight,
                        selection.Type,
                        3 as Relevance
                    from
                        tblCars as selection
                    where
                        selection.Colour = 'black' and selection.Weight = 'light' and selection.Type = 'van'
                union all
                select
                        cars.CarId,
                        cars.Colour,
                        cars.Weight,
                        cars.Type,
                        count(*) as Relevance
                    from
                        tblCars as cars
                    inner join
                        (
                            select
                                    byColour.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byColour
                                on
                                    cars.Colour = byColour.Colour
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byColour.CarId <> cars.CarId
                            union all
                            select
                                    byWeight.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byWeight
                                on
                                    cars.Weight = byWeight.Weight
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byWeight.CarId <> cars.CarId
                            union all
                            select
                                    byType.CarId
                                from
                                    tblCars as cars
                                inner join
                                    tblCars as byType
                                on
                                    cars.Type = byType.Type
                                where
                                    cars.Colour = 'black' and cars.Weight = 'light' and cars.Type = 'van'
                                    and
                                    byType.CarId <> cars.CarId
                        ) as matches
                    on
                        cars.CarId = matches.CarId
                    group by
                        cars.CarId,
                        cars.Colour,
                        cars.Weight,
                        cars.Type
            ) as results
        order by
            Relevance desc
    

    Output:

    CarId   Colour  Weight  Type    Relevance
    1       black   light   van     3
    3       white   light   van     2
    4       blue    light   van     2
    5       black   medium  van     2
    6       white   medium  van     1
    7       blue    medium  van     1
    8       black   heavy   limo    1
    

提交回复
热议问题