简体   繁体   English

我的SQL查询有什么问题

[英]what's wrong with my sql query

Id like it to return records where products_extrafield_id is not 14 , but it still RETURNS it in my results .. I'm using joins 我希望它返回product_extrafield_id不为14的记录,但它仍会在我的结果中返回它。

SELECT op. * 
FROM 
  products_to_products_extra_fields AS p
  INNER JOIN orders_products AS op ON p.products_id = op.products_id
  INNER JOIN orders AS o ON op.orders_id = o.orders_id
WHERE NOT 
EXISTS (

    SELECT * 
    FROM products_to_products_extra_fields
    WHERE
      p.products_id = op.products_id
      AND p.products_extra_fields_id = 14
  )
  AND o.date_purchased BETWEEN  '2013-11-29' AND  '2013-12-03 23:59:59'
  AND o.payment_method =  'Institutional Billing'
  AND o.orders_status <100001
GROUP BY o.orders_id
ORDER BY DECODE( o.cc_type,  'oFsAfHr7' ) ASC 
LIMIT 0 , 30

If I am reading your SQL correctly, then you don't need a NOT EXISTS clause to do this. 如果我正确地阅读了您的SQL,那么您就不需要NOT NOTISTS子句了。 Just replace the NOT EXISTS clause with the following statement: p.products_extra_fields_id != 14 只需用以下语句替换NOT EXISTS子句:p.products_extra_fields_id!= 14

SELECT 
    op. * 
FROM 
    products_to_products_extra_fields AS p
    INNER JOIN orders_products AS op 
    ON p.products_id = op.products_id
    INNER JOIN orders AS o 
    ON op.orders_id = o.orders_id
    WHERE 
        p.products_extra_fields_id != 14
    AND o.date_purchased BETWEEN  '2013-11-29' AND  '2013-12-03 23:59:59'
    AND o.payment_method =  'Institutional Billing'
    AND o.orders_status <100001
GROUP BY o.orders_id
ORDER BY DECODE( o.cc_type,  'oFsAfHr7' ) ASC 
LIMIT 0 , 30

Why are you GROUPing without using aggregate functions? 为什么在不使用聚合函数的情况下进行分组? Also, why would try to GROUP BY a field that is not in your SELECT? 另外,为什么要尝试对不在您的SELECT中的字段进行GROUP BY? I added the DISTINCT operator, along with moving your logic around. 我添加了DISTINCT运算符,并移动了逻辑。 You subquery was using criteria from the main/ outer query. 您的子查询使用的是来自主/外部查询的条件。

SELECT DISTINCT o.cc_type, op.* 
FROM  orders_products AS op 
  INNER JOIN orders AS o 
    ON op.orders_id = o.orders_id
WHERE o.date_purchased BETWEEN  '2013-11-29' AND  '2013-12-03 23:59:59'
  AND o.payment_method =  'Institutional Billing'
  AND o.orders_status <100001
  AND NOT EXISTS (SELECT *
                  FROM products_to_products_extra_fields AS p
                  WHERE p.products_id = op.products_id
                    AND p.products_extra_fields_id = 14)
ORDER BY DECODE( o.cc_type,  'oFsAfHr7' ) ASC 
LIMIT 0 , 30;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM