数据库查询2张表
Database query on 2 tables
我有 2 个 table(tag_details 和 movie_tags),我想查询那些 tag_details_id (tag_details) 不属于的部分的 movie_tags table。我怎样才能轻松完成这项工作?
您可以使用子查询轻松完成此操作。
Select * from tag_details
where tag_details_id not in
(Select tag_details_id from movie_tags)
您必须 select 所有 tag_details_Id
不在 movie_tags
table 中。这可以通过如下子查询来实现:
select * from tag_details where tag_details_Id not in
(select tag_details_Id from movie_tags);
了解如何使用 outer joins!
Select *
from tag_Details TD
LEFT JOIN movie_tags MT
on TD.Tag_Details_ID = MT.Tag_Details_ID
WHERE MT.Tag_Details_Id is null
或者如果您更改 table 顺序,则作为右连接...
Select *
from movie_tags MT
RIGHT JOIN tag_Details TD
on TD.Tag_Details_ID = MT.Tag_Details_ID
WHERE MT.Tag_Details_Id is null
这表示 return 电影标签中的所有 tag_details 和相关记录,但前提是电影标签为 NULL(这是由于数据丢失造成的,因此是您所追求的)
试试这个:
SELECT tag_details_id
FROM tag_details
MINUS
SELECT tag_details_id
FROM movie_tags;
我有 2 个 table(tag_details 和 movie_tags),我想查询那些 tag_details_id (tag_details) 不属于的部分的 movie_tags table。我怎样才能轻松完成这项工作?
您可以使用子查询轻松完成此操作。
Select * from tag_details
where tag_details_id not in
(Select tag_details_id from movie_tags)
您必须 select 所有 tag_details_Id
不在 movie_tags
table 中。这可以通过如下子查询来实现:
select * from tag_details where tag_details_Id not in
(select tag_details_Id from movie_tags);
了解如何使用 outer joins!
Select *
from tag_Details TD
LEFT JOIN movie_tags MT
on TD.Tag_Details_ID = MT.Tag_Details_ID
WHERE MT.Tag_Details_Id is null
或者如果您更改 table 顺序,则作为右连接...
Select *
from movie_tags MT
RIGHT JOIN tag_Details TD
on TD.Tag_Details_ID = MT.Tag_Details_ID
WHERE MT.Tag_Details_Id is null
这表示 return 电影标签中的所有 tag_details 和相关记录,但前提是电影标签为 NULL(这是由于数据丢失造成的,因此是您所追求的)
试试这个:
SELECT tag_details_id
FROM tag_details
MINUS
SELECT tag_details_id
FROM movie_tags;