mysql一对多反连接查询
mysql one-to-many anti join query
寻求有关此情况查询的帮助:
我有 3 个 table。 Shoe table 是一款独特的鞋款合集。颜色 table 是鞋子可能具有的独特颜色集合。 shoe_color table 是一个连接 table。一种鞋款可以有多种颜色,也可以只有一种。
我想查询不包含特定颜色的鞋款。例如... Shoe 1
具有颜色 black
、red
、white
。 Shoe 2
具有颜色 black
、white
。 Shoe 3
具有颜色 black
、white
。查询不包含颜色 red
应该 return 行:Shoe 2
和 Shoe 3
.
感谢任何帮助,如有任何问题请提出澄清!
您可以使用 not exists
.
假设连接 table 通过主键(例如 color_id
和 shoe_id
)引用其他 table,并且该列 color_name
在 table color
存储颜色名称,你会去:
select s.*
from shoe s
where not exists (
select 1
from shoe_color sc
inner join color c on c.color_id = sc.color_id
where sc.shoe_id = s.shoe_id and c.color_name = 'red'
)
寻求有关此情况查询的帮助:
我有 3 个 table。 Shoe table 是一款独特的鞋款合集。颜色 table 是鞋子可能具有的独特颜色集合。 shoe_color table 是一个连接 table。一种鞋款可以有多种颜色,也可以只有一种。
我想查询不包含特定颜色的鞋款。例如... Shoe 1
具有颜色 black
、red
、white
。 Shoe 2
具有颜色 black
、white
。 Shoe 3
具有颜色 black
、white
。查询不包含颜色 red
应该 return 行:Shoe 2
和 Shoe 3
.
感谢任何帮助,如有任何问题请提出澄清!
您可以使用 not exists
.
假设连接 table 通过主键(例如 color_id
和 shoe_id
)引用其他 table,并且该列 color_name
在 table color
存储颜色名称,你会去:
select s.*
from shoe s
where not exists (
select 1
from shoe_color sc
inner join color c on c.color_id = sc.color_id
where sc.shoe_id = s.shoe_id and c.color_name = 'red'
)