gpt4 book ai didi

mysql - 连接查询以获取与一列中的 2 个值匹配的记录

转载 作者:行者123 更新时间:2023-11-29 16:32:03 26 4
gpt4 key购买 nike

所以,我有一个电影表(movie)、一个类别表(cat)和它们的关系表(movie_cat)。
“电影”表有值

id  label     
1 Ironman

“cat”表有值

id  label      value
1 Genre Action
2 Language English

“movie_cat”表有值

id  movie_id  cat_id
1 1 1
2 1 2

I need a query that can give me the list of movies which are in "Action" and "English" category.

最佳答案

直接的方法:

select *
from movie
where id in
(select movie_id from movie_cat where cat_id =
(select id from cat where label = 'Genre' and value = 'Action'))
and id in
(select movie_id from movie_cat where cat_id =
(select id from cat where label = 'Language' and value = 'English'));

聚合方法:

select *
from movie
where id in
(
select movie_id
from movie_cat
where cat_id in (select id from cat where (label, value) in (('Genre', 'Action'),
('Language', 'English'))
group by movie_id
having count(*) = 2
);

关于mysql - 连接查询以获取与一列中的 2 个值匹配的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53804135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com