MYSQL:哪里没有语法错误

时间:2014-02-15 22:13:53

标签: mysql sql

我想知道如何在mysql中使用WHERE NOT EXISTS进行以下查询。它给出了mysql语法错误。我尝试了所有可能的方法,但没有任何帮助。

SELECT * 
  FROM (
        (
         SELECT l.pid 
           FROM lineitem l 
          WHERE l.oid IN (
                          SELECT o1.oid 
                            FROM customer c 
                                    JOIN ( 
                                          SELECT o.oid,
                                                 o.cid 
                                            FROM orders o
                                         ) o1 
                                         ON c.cid=o1.cid 
                                        AND c.city='Newark'
                          ) 
       GROUP BY l.pid
        ) 
       WHERE not exists (
                        SELECT l.pid 
                          FROM lineitem l 
                         WHERE l.oid IN (
                                         SELECT o1.oid 
                                           FROM customer c 
                                                    JOIN (
                                                          SELECT o.oid,
                                                                 o.cid 
                                                            FROM orders o
                                                          )o1 
                                                          ON c.cid=o1.cid
                                                          AND c.city<>'Newark'
                                         )
                        )
       )

我的表是: 客户(CID,名称,城市,州) 订单(OID,CID,日期) 产品(PID,价格,产品名) 订单项(盖子,数量,OID,PID,totalprice)

我想选择仅由纽瓦克人购买的所有产品。我的逻辑是(选择纽瓦克客户购买的产品) - (选择所有其他客户购买的产品)。我使用WHERE NOT EXISTS因为差异关键字在mysql中不可用。

2 个答案:

答案 0 :(得分:2)

这是一个更好的解决方案:

SELECT ProductID FROM (
       SELECT p.pid ProductID
         FROM lineitem l
              JOIN product p
                   ON l.pid = p.pid
              JOIN orders o
                   ON l.oid = o.oid
        WHERE o.cid IN (
              SELECT c.cid 
                FROM customer c 
               WHERE c.city = 'Newark'
              )
        ) t1 
WHERE ProductID NOT IN (
      SELECT p.pid 
        FROM lineitem l
             JOIN product p
                  ON l.pid = p.pid
         JOIN orders o
              ON l.oid = o.oid
       WHERE o.cid IN (
             SELECT c.cid 
               FROM customer c 
              WHERE c.city <> 'Newark'
             )
      );

我也相信,由于嵌套查询较少,因此在性能方面会更好。

您的查询中的问题是括号。你说

SELECT * 
  FROM ( 
         ( ... ) 
        WHERE NO EXISTS 
         ( ... ) 
       );

虽然你应该说

SELECT * 
  FROM ( ... ) 
WHERE NO EXISTS ( ... );

由于它有效,这里有一个进一步的简化(删除派生表t1和第二部分子查询中的JOIN product p):

SELECT p.pid ProductID
  FROM lineitem l
       JOIN product p
            ON l.pid = p.pid
       JOIN orders o
            ON l.oid = o.oid
 WHERE o.cid IN (
       SELECT c.cid 
         FROM customer c 
        WHERE c.city = 'Newark'
       )
  AND p.pid NOT IN (
      SELECT l.pid 
        FROM lineitem l
         JOIN orders o
              ON l.oid = o.oid
       WHERE o.cid IN (
             SELECT c.cid 
               FROM customer c 
              WHERE c.city <> 'Newark'
             )
      );

答案 1 :(得分:0)

Select l.pid from lineitem l
inner join Orders o on l.oid = o.oid
inner join Customers c on o.cid = c.cid  and c.city = 'Newark'
where not in 
(
Select ol.pid from lineitem ol
inner join Orders oo on ol.oid = oo.oid
inner join Customers oc on oo.cid = oc.cid  and oc.city <> 'Newark'
)

我想...... 如果不存在则需要相关的子查询。