如何查看另一个表中是否有两列的组合?

时间:2019-04-06 17:22:26

标签: mysql

我有两个表:Transit和Connect。

Transit
TransitType    TransitRoute    TransitPrice
--------------------------------------------

Connect
SiteName       TransitType     TransitRoute
--------------------------------------------

我想检查Transit表中TransitType和TransitRoute的组合是否在Connect表中。

我当前的命令是这样:

    TransitRoute IN 
    (SELECT connect.TransitRoute from connect WHERE connect.SiteName = sname)

我也尝试了以下命令,但收到一个错误消息,说Operand应该包含1列。

    SELECT * FROM transit WHERE ((transitRoute, transitType) IN (SELECT
    (connect.TransitRoute, connect.TransitType) from connect WHERE connect.SiteName = sname) 

1 个答案:

答案 0 :(得分:0)

使用JOIN

SELECT t.*
FROM Transit AS t
JOIN Connect AS c ON t.TransitType = c.TransitType AND t.TransitRoute = c.TransitRoute

由于您要匹配的列具有相同的名称,因此您也可以使用USING来缩写:

SELECT t.*
FROM Transit AS t
JOIN Connect USING (TransitType, TransitRoute)