简体   繁体   English

MYSQL SELECT WHERE CLAUSE-排除行

[英]MYSQL SELECT WHERE CLAUSE - exclude rows

In MYSQL, let's say I have following two tables. 在MYSQL中,假设我有以下两个表。

"orders": “命令”:

id   |    name    | customer_id |    type   |  id_fabric
--------------------------------------------------------
1    | "P1601"    |      0      |     1     |    1    
2    | "M6451"    |      0      |     2     |    2    
3    | "T8200"    |      8      |     1     |    1    
4    | "R7441"    |      0      |     2     |    2    
5    | "S8018"    |      2      |     1     |    3    
6    | "P1240"    |      7      |     1     |    3    

"fabrics": “面料”:

id    |  color  |  remaining 
----------------------------
1     | black   |  3.40 
2     | red     |  16 
3     | navy    |  12 

I would like to create a query to retrieve only the fabrics whose remaining is greater than 5, which don't have any orders with customer_id = 0 of and whose order type is not equal to 2 我想创建一个查询以仅检索剩余大于5的面料,这些面料没有任何customer_id = 0的订单,并且其订单类型不等于2

So in this case the result would be: 因此,在这种情况下,结果将是:

id  | color   |  remaining
------------------------------------------------
3   | navy    |  12

I've tried to achieve this by using this following Sql query, but it doesn't get the expected result: 我已经尝试通过使用以下Sql查询来实现这一点,但没有得到预期的结果:

SELECT
    color,
    remaining
FROM
    fabrics
LEFT JOIN orders ON id_fabric = id
WHERE
    remaining > 2 AND id IN(
    SELECT
        id_fabric
    FROM
        orders
    WHERE
        type != 2 AND customer_id != 0
)
GROUP BY
    id

Any idea ? 任何想法 ?

Use NOT EXISTS 使用NOT EXISTS

SELECT
    f.color,
    f.remaining
FROM fabrics f
WHERE f.remaining > 5 AND 
    NOT EXISTS(
        SELECT 1
        FROM orders o
        WHERE o.id_fabric = f.id AND
              o.type = 2 AND o.customer_id = 0
    )

Note that also JOIN and GROUP BY can be omitted from your original query. 请注意,原始查询中也可以省略JOIN和GROUP BY。

try like below 尝试如下

SELECT
    color,
    remaining
FROM
    fabrics
 JOIN orders ON id_fabric = id
where customer_id!=0 and remaining>5 and type!=2

Using JOIN and WHERE condition and 'GROUP BY` 使用JOINWHERE条件和“ GROUP BY”

id is same filed in both tables so use the alies name for tables. id在两个表中都相同,因此对表使用alies名称。

select f.id,f.color,f.remaining
from fabrics f
join orders o on o.id_fabric  = f.id
where f.remaining > 2 and o.type !=2 and o.customer_id!=0
group by f.id

I don't think that subselect in your where condition is necessary in this case. 在这种情况下,我认为不需要在需要条件的地方进行子选择。 You could simply go ahead like 你可以简单地像

SELECT
    color,
    remaining
FROM
    fabrics
LEFT JOIN orders ON id_fabric = id
WHERE
    remaining > 2  
    AND customer_id!=0
    AND type != 0
GROUP BY
    id 

You can try using correlated subquery 您可以尝试使用相关子查询

SELECT
    color,
    remaining
FROM
    fabrics
LEFT JOIN orders ON id_fabric = id where remaining > 5 and 
not exists (select 1 from orders o where o.id_fabric=fabrics.id and o.customer_id=0 and o.type=2)

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

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