[英]Select all in table where a column in table 1 exists in another table and a second column equals a variable in second table
I know the title is confusing but its the best I could explain it. 我知道标题令人困惑,但它是我能解释的最好的。 Basically im developing a cinema listings website for a company which owns two cinemas. 基本上我正在为拥有两家电影院的公司开发一个电影院列表网站。 So I have a database which has the two tables "Films" and "Listings" with data for both cinemas in them. 所以我有一个数据库,其中包含两个表“Films”和“Listings”,其中包含两个电影院的数据。
I'm trying to select all films and their data for one cinema if the films name shows up in the listings (since the two cinemas share all films but in the table but the may not have the same films showing) 如果电影名称出现在列表中,我试图为一部电影选择所有电影和他们的数据(因为两部电影院共享所有电影,但在表格中但可能没有相同的电影显示)
Here is what i have come up with but I run into a problem as when the "SELECT DISTINCT" returns more than one result it obviously cant be matched with the FilmName on tbl Films. 这是我想出来的但是我遇到了一个问题,因为当“SELECT DISTINCT”返回多个结果时,它显然无法与tbl Films上的FilmName匹配。 How can i check this value for all FilmNames on tblFilms? 如何检查tblFilms上所有FilmNames的值?
SELECT *
FROM tblFilms
WHERE FilmName = (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
use IN
if the subquery return multiple values, 如果子查询返回多个值,则使用IN
,
SELECT *
FROM tblFILMS
WHERE FilmName IN (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
Another way to solve thius is by using JOIN
( which I recommend ) 解决thius的另一种方法是使用JOIN
( 我推荐 )
SELECT DISTINCT a.*
FROM tblFILMS a
INNER JOIN tblListings b
ON a.FilmName = b.FilmName AND
b.Cimema = 1
for faster query execution, add an INDEX
on FilmName
on both tables. 为了更快地执行查询, FilmName
在两个表上的FilmName
上添加一个INDEX
。
If you have your schemas for the tables, that would help. 如果您有表格的模式,那将有所帮助。
That said, I believe what you want to look at is the JOIN keyword. 也就是说,我相信你想要看的是JOIN关键字。 (inner/outer/left/etc). (内/外/左/等)。 That's exactly what JOIN is meant to do (ie your title). 这正是JOIN的意图(即你的头衔)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.