0

我有 2 张桌子:

Customers:
- ID
- NAME

Modulemembers:
- ID
- customer_id
- enabled

我使用它来为某些客户启用我的网站的功能。我有一个表单,站点管理员可以在其中为客户添加模块,所以我需要一个查询来查找不是 modulemembers 表成员的客户。

我做的:

SELECT customers.id, 
       customers.name 
FROM   customers, 
       modulemembers 
WHERE  customers_id != modulemembers.cust_id 
ORDER  BY customers.name 

但它不起作用。我究竟做错了什么?

4

3 回答 3

4

您可以NOT EXISTSWHERE子句中使用来获得结果:

SELECT c.id, 
  c.name 
FROM customers c
WHERE not exists (select customer_id
                  from modulemembers m
                  where c.id = m.customer_id)
order by c.name
于 2013-03-11T20:52:02.147 回答
2

您可以使用 aLEFT OUTER JOIN和基于 的过滤器NULLs。下面的查询将从customers表和modulemembers表中提取所有结果。modulemembers如果表中没有匹配项,custid则为NULL

SELECT c.id, c.name 
FROM customers c
    LEFT OUTER JOIN modulemembers m ON c.customers_id = m.cust_id 
WHERE m.custid IS NULL
ORDER BY c.name
于 2013-03-11T20:48:03.353 回答
0

尝试这样的事情:

   SELECT c.ID AS customerId,
          c.NAME AS fullName,
          m.ID AS memberId
     FROM Customers AS c
LEFT JOIN Modulemembers AS m ON m.customer_id = c.ID
    WHERE m.ID IS NULL;
于 2013-03-11T20:49:11.457 回答