简体   繁体   English

在表中选择all表,其中表1中的列存在于另一个表中,第二列等于第二个表中的变量

[英]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.

相关问题 从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量 - Select all columns from one table and one from another where column equals variable 第二个表中不存在的地方 - where not exists in second table mysql从第一个表中的列更新第二个表中列的行,其中第一个表中的另一列与第二个表中的列匹配 - mysql update rows of column in second table from column in first table where another column in first table matches column in second table SQL-从表中选择列,其中另一个表的列值等于列名列表 - SQL - Select Columns from table where another table column value equals column name list 从表中选择@variable in(表列) - select from table where @variable in(table column) 在另一个表中按重量乘以列,需要在SQL中进行第二次选择和求和 - Multiplying column by weight in another table requiring a second select and summing in SQL Linq:从2个数据表中选择,其中第一个表中的列ID =第二个表中的列ID - Linq: Select from 2 datatable where column id from first table = column id from second table SQL SELECT 并检查第二个表中的列 - SQL SELECT and check column from second table 表中的 Select 行,其中 id 等于另一个表中的另一个 id,并对结果列中的值求和 - Select rows in a table where id is equals to another id in another table and sum values in column from the result Select 第一个表中的行,第二个表中的相同列值 - Select Rows From First Table, For Same Column Values in Second Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM