简体   繁体   English

sql 只返回具有特定列值的行

[英]sql only return rows that have certain column values

在此处输入图像描述

With my tables above how can I return the user_id (s) which belong to companies that are only in type_id = 34 and 35 and not belonging in type_id = 8. So since comp_id = 3 isnt in type_id = 8 and is in type_id = 34 and 35 therefore results should be user_id =104 and 105使用上面的表格,我如何返回属于仅在type_id = 34 和 35 中且不属于type_id = 8 的公司的user_id (s)。因此,由于comp_id = 3 不在type_id = 8 中并且在type_id = 34 中和 35 因此结果应该是user_id =104 和 105

Im looking for users who are in companies both type_id =34 and 35 not either or.我正在寻找type_id = 34 和 35 都不是 or 的公司中的用户。 If it is not in 34 but in 35 then users from that company should not be returned.如果不是在 34 中而是在 35 中,则不应返回该公司的用户。

You can use conditional aggregation to get the comp_id s that meet your conditions and with the operator IN get the user_id s:您可以使用条件聚合来获取满足您条件的comp_id并使用运算符IN获取user_id

select user_id 
from Table_2
where comp_id in (
  select comp_id
  from Table_1
  group by comp_id
  having sum(type_id not in (34, 35)) = 0
     and sum(type_id in (34, 35)) = 2
)

If there are other type_id s than 34, 35 and 8 and they are also allowed as long as 34 and 35 exist but not 8 then:如果除了 34、35 和 8 之外还有其他type_id并且只要存在 34 和 35 但不存在 8,它们也被允许:

select user_id 
from Table_2
where comp_id in (
  select comp_id
  from Table_1
  group by comp_id
  having sum(type_id = 8) = 0
     and sum(type_id in (34, 35)) = 2
)

This is a simple and readable way to do it.这是一种简单易读的方法。

SELECT DISTINCT user_id 
FROM Table_2
WHERE comp_id IN (
    SELECT DISTINCT comp_id
    FROM Table_1
    WHERE type_id IN (34, 35)
    MINUS
    SELECT DISTINCT comp_id
    FROM Table_1
    WHERE type_id IN (8)
)

If your database doesn't support SELECT DISTINCT then just use SELECT and add a GROUP BY comp_id to the bottom of each SELECT statement.如果您的数据库不支持 SELECT DISTINCT,那么只需使用 SELECT 并将 GROUP BY comp_id 添加到每个 SELECT 语句的底部。 Capitalisation of keywords is optional.关键字的大小写是可选的。 If you already have a big WHERE statement for Table_2 you can use an inner join to the sub-select instead of an IN.如果您已经有一个用于 Table_2 的大 WHERE 语句,您可以使用子选择的内部连接而不是 IN。

-- EDIT: to avoid the use of MINUS for MySQL -- -- 编辑:避免对 MySQL 使用 MINUS --
Confession: I'm not able to test this SQL in MySQL at the moment忏悔:我目前无法在 MySQL 中测试这个 SQL

SELECT DISTINCT Table_2.user_id 
FROM Table_2
INNER JOIN
     (SELECT DISTINCT comp_id
      FROM Table_1
      WHERE type_id IN (34, 35)
     ) type_include ON Table_2.type_id = type_include.type_id
LEFT JOIN
     (SELECT DISTINCT comp_id
      FROM Table_1
      WHERE type_id IN (8)
     ) type_exclude ON Table_2.type_id = type_exclude.type_id
                   AND type_exclude.type_id IS NULL

You can do such a thing with a subquery, in which you specify the condition that should not be met你可以用一个子查询来做这样的事情,你可以在其中指定不应该满足的条件

SELECT DISTINCT t2.user_id 
    FROM Table_2 t2 
JOIN Table_1 t1 
    ON ((t1.comp_id = t2.comp_id) 
      AND ((type_id = 34) OR (type_id = 35))
      AND t1.comp_id NOT IN (SELECT comp_id FROM Table_1 WHERE type_id = 8)
    )
;

PS: For the data in your example, you would not even have to check for the ID being equal to 35 or 34 as there are no other values in your data anyway. PS:对于您示例中的数据,您甚至不必检查 ID 是否等于 35 或 34,因为无论如何您的数据中都没有其他值。

SELECT DISTINCT t2.user_id 
    FROM Table_2 t2 
    WHERE t2.comp_id NOT IN (SELECT comp_id FROM Table_1 WHERE type_id = 8)
;

EDIT: As was correctly pointed out, the first query will编辑:正如正确指出的那样,第一个查询将

  • also include user_ids for which Table_1 only has rows with either type_id = 34 or type_id = 35 and not only with both还包括 user_ids,其中 Table_1 仅具有type_id = 34type_id = 35的行并且不仅具有两者
  • also include user_ids for which Table_1 has rows that additionally to 34 or 35 may have another type_id other than 8 (which is not present in the example, but may happen in other data)还包括 user_ids,其中 Table_1 具有除 34 或 35 之外的行可能具有除 8 之外的另一个type_id (示例中不存在,但可能发生在其他数据中)

This is by design, as I understood the question requiring this.这是设计使然,因为我理解需要这个的问题。 If this is not the intended result, please look at the answer from @forpas ( https://stackoverflow.com/a/63382574/14015737 ), which yields a result that does neither of the above.如果这不是预期的结果,请查看@forpas 的答案( https://stackoverflow.com/a/63382574/14015737 ),这会产生上述两种情况都没有的结果。

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

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