Small Part of the Instructor Table
--------------------------------------------------
Instructor_ID Salutation First Last Street Address Zip
101 Mr Fernand Hanks 100 East 87th 10015
102 Mr Tom Wojick 518 West 120th 10025
103 Ms Nina Schorin 210 West 101st 10025
104 Mr Gary Pertez 34 Sixth Ave 10035
105 Ms Anita Morris 34 Maiden Lane 10015
106 Rev Todd Smythe 210 West 101st 10025
107 Dr Marilyn Frantzen 254 Bleeker 10005
108 Mr Charles Lowry 518 West 120th 10025
109 Hon Rick Chow 56 10th Avenue 10015
110 Ms Irene Willig 415 West 101st (null)
------------------------------------------------------------------
Small Part of the Student Table
152 Mr. Thomas Edwards 501 W. Elm 07036
157 Ms. Shirley Jameson 101 Daniel St. 07008
158 Mr. Roy Limate 5 Horizon Rd. 07024
159 Mr. Thomas Edwards 45 Maplewood Ave. 07040
160 Mr. John T. Beitler 100 Plaza Dr, ITT CSI Emp. Dpt 07096
168 Ms. Sally Naso 812 79th St. 07047
182 Mr. Jeffrey Delbrun PO Box 1091 07024
185 Mr. Dennis Mehta 371 Monmouth St. 07302
186 Ms. Christine Sheppard 16 Seymour St. 07042
187 Mr. O. Garnes 125 Great Hills Rd 07078
使用 UNION 集合运算符编写一个集合操作,以列出 INSTRUCTOR 和 STUDENT 表中所有重复的邮政编码。
这是我想出来的,不知道对不对。
select zip, count(*) from instructor
group by zip
having count(*) > 1
union
select zip, count(*) from student
group by zip
having count(*)>1
请帮忙!!
OUTPUT:
Zip Count(*)
06820 3
06830 3
06880 2
06903 2
07003 2
07010 6
07011 2
07012 2
07016 2
07024 9
最佳答案
您的解决方案基本上是这样做的:
- 识别 INSTRUCTOR 中的骗子
- 识别学生中的骗子
- 同时显示它们(即 UNION 或 UNION ALL)
我认为你做对了。
但是如果我像下面这样理解这个问题,陈述就会不同:
- 构建来自 INSTRUCTOR 和 STUDENT 的所有 ZIP 的联合
- 现在识别受骗者
例如,只有一个 INSTRUCTOR 的 ZIP=99999,正好有一个 STUDENT 的 ZIP=99999,那么您的解决方案不会将他们显示为受骗者。 根据对问题的后一种理解,这很可能是一个骗局。
对于后一种情况,语句将/可能是:
select x.zip from
(select zip from instructor
union all
select zip from student) x
group by x.zip
having count(*) > 1;
关于mysql - SQL 语句仅显示来自两个不同表的重复邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094991/