简体   繁体   English

SQL:基于多个记录的查询

[英]SQL: Query based on multiple records

The following table has the ID number of people along with cities they have worked in: 下表列出了ID号码以及工作过的城市:

PERSON_NO | CITY_NAME
---------------------
1         |    City A
2         |    City B
3         |    City A
3         |    City B
3         |    City C
4         |    City A
4         |    City B
4         |    City C

How would I be able to get the PERSON_NO of all the people who have lived in all three cities, A,B, and C? 我如何才能获得分别居住在A,B和C这三个城市中的所有人的PERSON_NO?

I want to return 我想回来

PERSON_NO
---------
3
4

Thanks, again. 再次感谢。 I haven't had that much experience with SQL and so I'm not sure what to do. 我在SQL方面没有太多的经验,所以我不确定该怎么做。

If you're in SQL Server 2000 or higher you can use INTERSECT: 如果您使用的是SQL Server 2000或更高版本,则可以使用INTERSECT:

select PERSON_NO from table1 where CITY_NAME='City A'
intersect
select PERSON_NO from table1 where CITY_NAME='City B'
intersect
select PERSON_NO from table1 where CITY_NAME='City C'

Another way is to just join the table to itself a few times. 另一种方法是将表自身连接几次。

SELECT DISTINCT Person_No FROM mytable 
INNER JOIN mytable mt2 on (cityname='city b' and mt1.person_no=mt2.person_no)
INNER JOIN mytable mt3 on (cityname='city c' and mt1.person_no=mt3.person_no)
WHERE cityName='city a'

When first learning SQL most students don't realize it is completely legal to include the same table in a query multiple times. 第一次学习SQL时,大多数学生没有意识到多次在查询中包含同一张表是完全合法的。 There are lots of problems that can be solved this way. 通过这种方式可以解决很多问题。

You can use multiple EXISTS() clauses: 您可以使用多个EXISTS()子句:

   select a.PERSON_NO
   from personTable a
   where exists (select 1 from personTable where PERSON_NO = a.PERSON_NO
                 and CITY_NAME = 'City A')
   and exists (select 1 from personTable where PERSON_NO = a.PERSON_NO
                 and CITY_NAME = 'City B')
   and exists (select 1 from personTable where PERSON_NO = a.PERSON_NO
                 and CITY_NAME = 'City C')

I would do this with an aggregation and a HAVING . 我将通过聚合和HAVING Assuming no repeats: 假设没有重复:

 SELECT person_no, count(*)
   FROM mytable
  WHERE city_name IN ('City A', 'City B', 'City C')
  GROUP BY person_no
 HAVING count(*) = 3

This has the advantage of hitting the table only once, which a join might not do. 这样的优点是只打了一次表,而联接可能不会。

Didn't actually test it, but this general idea should work: 并没有实际测试过,但是这个基本思路应该可行:

SELECT DISTINCT PERSON_ID
FROM YOUR_TABLE T1
WHERE
    NOT EXISTS (
        SELECT CITY_NAME -- All cities.
        FROM YOUR_TABLE T2
        EXCEPT -- Or may be "MINUS", depending on your DBMS.
        SELECT CITY_NAME -- Cities the user worked in.
        FROM YOUR_TABLE T3
        WHERE T1.PERSON_NO = T3.PERSON_NO
    )

In plain English: 简而言之:

  • For every distinct person, find the set difference between all cities and cities she worked in . 对于每个不同的人,找出所有城市她工作过的城市之间的既定差异
  • If that difference is empty, the person has worked in all cities. 如果这种差异是空的,那么这个人已经在所有城市工作过。

Try this 尝试这个

SELECT   PERSON_NO 
FROM     YOURTABLENAME
WHERE    CITY_NAME =  'City A'
         AND  CITY_NAME = 'City B'
         AND  CITY_NAME = 'City C'

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

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