mysql> SELECT DISTINCT s1.job_id FROM skill_usage AS s1 -> INNER JOIN skill_usage AS s2 ON s1.job_id = s2.job_id -> WHERE s1.skill_id IN -> AND s2.skill_id IN -------- | job_id | -------- | 3 || 4 || 6 | -------- 3 rows in set (0.00 sec) - Z% `+ _8 |4 T* v$ C) L% \% X# f
我做错了什么?如何阅读我的查询?我想是时候写一本好书或 了Udemy 课程,但我没有自己的封面。' q! k, G+ ^: G% w( v
正确找到我的查询job_id= 6,但是错了(IMO)找到了job_id3 和 4AND条款。- P% f! @- H+ K i
% l# q4 ^5 @& \# h# x, i2 f解决方案: 9 r9 Z3 A' Z$ P- g/ q+ @+ d3 r 您可以在这里使用聚合物: - K# i7 l: m! p
SELECT job_idFROM skill_usageWHERE skill_id IN (3,4)GROUP BY job_idHAVING MIN(skill_id) MAX(skill_id);; C( [; O* [& D4 G* Q) t! }3 a
本查询应受益于以下索引: , B c/ S+ e, _% H# [
CREATE INDEX idx ON skill_usage (skill_id,job_id);/ \! o# E- j0 k# Q, n' A