MySql查明记录的id是否在另一个表中(在一个查询中)

时间:2014-03-15 19:13:32

标签: mysql select

假设我有2个表:

Table 1: (customers)
------------------------------------------
id | name | etc... | etc..


Table 2: (blockList)
------------------------
id

我想知道每个客户是否存在于blockList表中,因为我正在通过customers表循环(在单个查询中,作为一个单独的字段)

像这样:SELECT * FROM customers, blockList ORDER BY id DESC

2 个答案:

答案 0 :(得分:1)

您需要使用join,例如:

SELECT c.*, b.id AS id_blocklist
FROM customers AS c
LEFT JOIN blocklist AS b ON b.id = c.id
ORDER BY c.id DESC

如果您只想要阻止列表中的记录,请使用INNER JOIN

答案 1 :(得分:0)

您必须指定表格连接

SELECT * FROM customers as c, blockList as b WHERE c.id = b.id ORDER BY id DESC