MySQL在查询中-按匹配顺序 [英] MySQL WHERE IN Query - ORDER BY Match
问题描述
我想重新表达一下我的问题,因为我的最后一个答案不是所有人都清楚.
这是我的测试表
+----------+---------+-------+
| rel_id | genre | movie |
+----------+---------+-------+
| 1 | 1 | 3 |
| 2 | 8 | 3 |
| 3 | 3 | 3 |
| 4 | 2 | 5 |
| 5 | 8 | 5 |
| 6 | 3 | 5 |
| 7 | 1 | 8 |
| 8 | 8 | 8 |
| 9 | 3 | 8 |
| 10 | 5 | 9 |
| 11 | 7 | 9 |
| 12 | 9 | 9 |
| 13 | 4 | 9 |
| 14 | 12 | 9 |
| 15 | 1 | 10 |
| 16 | 8 | 10 |
| 17 | 3 | 10 |
| 18 | 5 | 10 |
| 19 | 1 | 11 |
| 20 | 2 | 11 |
| 21 | 8 | 11 |
| 22 | 5 | 11 |
| 23 | 3 | 11 |
+----------+---------+-------+
如果我寻找类型为1、8、3的电影,则结果应按以下顺序排列:电影编号. 3、8、10、5、11(9缺).
如果不可能的话,我只想要所有完全匹配的"1、8、3",在这种情况下,我只会得到电影编号. 3和8.
如果我理解正确,则希望按匹配数降序对结果进行排序.为此,您可以尝试:
SELECT movie
FROM genre_rel
WHERE genre IN (1, 8, 3)
GROUP BY movie
order by count(movie) desc
如果您想要符合所有条件的电影,则可以使用:
SELECT movie
FROM genre_rel
WHERE genre IN (1, 8, 3)
GROUP BY movie
HAVING count(movie) = 3
更新:
这是我在MySql中可以做的最好的事情.您无法使用IN,因为您无法提取有关过滤器顺序的信息.如果添加派生表作为过滤手段,则可以附加此信息并将其用于按位置匹配显示结果.请注意,您没有在genre_rel表中提供任何订购信息,因此您实际上并不了解每部电影的流派的重要性.此查询将按标准中流派的重要性降序为您提供匹配的电影:
SELECT movie
FROM genre_rel
INNER join
(
select 1 genre, 1000 weight
union all
select 8, 100
union all
select 3, 10
) weights
on genre_rel.genre = weights.genre
GROUP BY movie
order by sum(weight) desc
请注意,除5部电影外的所有电影都属于所有3种类型.如果您在genre_rel中添加一列表示重要性顺序的列,则可能会设计一些数学(权重-重要性或类似的东西).
I'm trying to rephrase my question, cause my last one wasn't clear to everyone.
This is my Test Table
+----------+---------+-------+
| rel_id | genre | movie |
+----------+---------+-------+
| 1 | 1 | 3 |
| 2 | 8 | 3 |
| 3 | 3 | 3 |
| 4 | 2 | 5 |
| 5 | 8 | 5 |
| 6 | 3 | 5 |
| 7 | 1 | 8 |
| 8 | 8 | 8 |
| 9 | 3 | 8 |
| 10 | 5 | 9 |
| 11 | 7 | 9 |
| 12 | 9 | 9 |
| 13 | 4 | 9 |
| 14 | 12 | 9 |
| 15 | 1 | 10 |
| 16 | 8 | 10 |
| 17 | 3 | 10 |
| 18 | 5 | 10 |
| 19 | 1 | 11 |
| 20 | 2 | 11 |
| 21 | 8 | 11 |
| 22 | 5 | 11 |
| 23 | 3 | 11 |
+----------+---------+-------+
Result should be in the following order if I look for movies with genre 1, 8, 3 : Movie no. 3, 8, 10, 5, 11 (9 is out).
If it's not possible then I just want all with the exact match "1, 8, 3", in that case I just would get movie no. 3 AND 8.
If I understand correctly you want to sort results by number of matches in descending order. To do so, you might try:
SELECT movie
FROM genre_rel
WHERE genre IN (1, 8, 3)
GROUP BY movie
order by count(movie) desc
And if you want movies that match all the criteria, you might use:
SELECT movie
FROM genre_rel
WHERE genre IN (1, 8, 3)
GROUP BY movie
HAVING count(movie) = 3
UPDATE:
This is the best I can do in MySql. You cannot use IN because you cannot extract information about order of filters. If you add derived table as a means of filtering, you can append this information and use it to show results by positional matches. Note that you do not provide any ordering info in genre_rel table so you don't really know the importance of genres per movie. This query will give you matching movies by descending order of importance of genres in criteria:
SELECT movie
FROM genre_rel
INNER join
(
select 1 genre, 1000 weight
union all
select 8, 100
union all
select 3, 10
) weights
on genre_rel.genre = weights.genre
GROUP BY movie
order by sum(weight) desc
Note that all the movies except 5 belong to all 3 genres. If you add a column to genre_rel representing order of importance you might devise some mathematics (weight - importance or something similar).
这篇关于MySQL在查询中-按匹配顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!