简体   繁体   English

SQL-在子查询中根据特定条件选择

[英]SQL - Select with specific conditions in sub-query

I'm trying to use a query to select all the data of a company that answers a specific condition but I have trouble doing so. 我正在尝试使用查询来选择能够满足特定条件的公司的所有数据,但是这样做很麻烦。 The following is what I have done so far: 以下是我到目前为止所做的:

SELECT *
  FROM company a
 WHERE a.id IN (SELECT b.company_id 
                  FROM provider b 
                 WHERE b.service_id IN (2, 4));

What I intend for the role of the sub-query (using the table below) to be, is to select the company_id that possess the service_id 2 and 4 . 我希望子查询(使用下表)的作用是选择具有service_id 2 4company_id

So in this example, it would only return the company_id 5 : 因此,在此示例中,它将仅返回company_id 5

+----------------+
| provider TABLE |
+----------------+

+----------------+----------------+----------------+
|       id       |   company_id   |   service_id   |
+--------------------------------------------------+
|        1       |        3       |        2       |
|        2       |        5       |        2       |
|        3       |        5       |        4       |
|        4       |        9       |        6       |
|        5       |        9       |        7       |
|       ...      |       ...      |       ...      |

As you may have guessed, the use of the IN in the sub-query does not fulfill my needs, it will select the company_id 5 but also the company_id 3 . 您可能已经猜到了,在子查询中使用IN不能满足我的需求,它将选择company_id 5 ,还选择company_id 3 I understand why, IN exists to check if a value matches any value in a list of values so it is not really what I need. 我知道为什么IN会检查一个值是否与值列表中的任何值匹配,所以这不是我真正需要的。

So my question is: 所以我的问题是:

How can I replace the IN in my sub-query to select company_id having the service_id 2 and 4 ? 如何替换子查询中的IN来选择具有service_id 24 company_id

The subquery should be: 子查询应为:

 SELECT b.company_id 
 FROM provider b 
 WHERE b.service_id IN (2, 4)
 GROUP BY  b.company_id 
 HAVING COUNT(b.service) = 2

You can self- JOIN the provider table to find companies that own both needed services. 您可以自我JOIN提供者表以查找同时拥有这两种所需服务的公司。

SELECT p1.company_id
FROM provider p1
INNER JOIN provider p2 on p2.company_id = p1.company_id and p2.service_id = 2
WHERE p1.service_id = 4

As well as the other answers are correct if you want to see all company detail instead of only Company_id you can use two EXISTS() for each service_id. 如果您想查看所有公司详细信息而不是仅查看Company_id,那么其他答案也是正确的,您可以为每个service_id使用两个EXISTS()

SELECT *
FROM Company C 
WHERE EXISTS (SELECT 1 
              FROM Provider P1 
              WHERE C.company_id = P1.company_id 
              AND P1.service_id = 2)
  AND EXISTS (SELECT 1 
              FROM Provider P2 
              WHERE C.company_id = P2.company_id 
              AND P2.service_id = 4)

I offer this option for the subquery... 我为子查询提供此选项...

SELECT b.company_id 
  FROM provider b WHERE b.service_id = 2
INTERSECT
SELECT b.company_id 
  FROM provider b WHERE b.service_id = 4

Often, I find the performance of these operations to be outstanding even with very large data sets... 通常,即使使用非常大的数据集,我也认为这些操作的性能非常出色。

  • UNION 联盟
  • INTERSECT 相交
  • EXCEPT (or MINUS in Oracle) 除(或Oracle中的减号)

This article has some good insights: 本文有一些不错的见解:

You Probably Don't Use SQL Intersect or Except Often Enough 您可能不使用SQL相交或仅足够使用

Hope it helps. 希望能帮助到你。

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

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