MYSQL从其他表执行切线查询?

时间:2013-01-15 13:55:42

标签: mysql sql select join

where meetingID = [[WHERE IT MATCHES]] (select meetingID from meeting where categoryID = '1')

我不太清楚要把这两个加在一起怎么样?

2 个答案:

答案 0 :(得分:0)

如果某个类别可以包含多个meetingID,则最好使用IN而不是=,这样就不会产生错误,

SELECT ...
FROM ....
where meetingID IN (select meetingID 
                    from meeting 
                     where categoryID = '1')

另一种方式是JOIN他们,例如

SELECT DISTINCT a.*
FROM   tableName a
       INNER JOIN meeting b
           ON a.meetingID = b.meetingID 
WHERE  b.categoryID  = '1'

答案 1 :(得分:0)

使用加入会提高效果。

SELECT ... 
FROM tablea a 
INNER JOIN meeting m ON a.meetingID = m.meetingID AND m.categoryID = '1'

SELECT ... 
FROM tablea a 
INNER JOIN (SELECT meetingID 
            FROM meeting 
            WHERE categoryID = '1'
          ) b ON a.meetingID = b.meetingID;

点击此链接MySQL Optimizing Subqueries