1

我有两个表,我需要使用 where 子句。表格1

CustomerID     Product
1              Car
2              Table
3              Golf
4              Foo
5              Yoo

表2

CustomeID      Comment
2              Three items
3              Returned
4              Complaint

我有一个查询,它在 where 语句中有两个过滤器,如下所示

Select * from table1 a left
join table2 b on a.customerid= b.customerid
where b.comment<>'Returned' and b.comment not like 'Three%'

当我运行查询时,我只得到了一条记录。我希望它还返回不在 table2 中的两个 customerID(三个记录)

4

4 回答 4

1
SELECT * FROM Table1 A
LEFT JOIN Table2 B
ON A.CustomerID=B.CustomeID      
WHERE ( B.COMMENT<>'RETURNED' AND B.COMMENT NOT LIKE 'THREE%')
OR B.CustomeID  IS NULL

我猜这应该可行。

于 2013-10-02T23:53:54.443 回答
0

尝试:

SELECT * 
FROM table1 a 
   LEFT JOIN table2 b on a.customerid= b.customerid 
WHERE (b.comment<>'Returned' and b.comment not like 'Three%') 
       OR (b.customerid is NULL)
于 2013-10-02T23:19:35.250 回答
0

我认为您需要使用 LEFT OUTER JOIN:

Select * from table1 a left outer join table2 b on a.customerid = b.customerid where b.comment<>'Returned' and b.comment not like 'Three%'
于 2013-10-02T23:33:14.230 回答
0

我通常不会加入那么多。

SELECT --It's a good practice to enumerate all rows you need, not just *.
   T1.CUSTOMERID,
   T1.PRODUCT
FROM 
   TABLE1 T1
WHERE
   T1.CUSTOMERID NOT IN (
      SELECT
         T2.CUSTOMERID
      FROM
         TABLE2 T2
      WHERE
         T2.COMMENT !='Returned' AND
         T2.COMMENT not like 'Three%')
于 2013-10-02T23:35:59.293 回答