Input
+--------+------+------+
| col1 | col2 | col3 |
+--------+------+------+
| apple | d | 10 |
| apple | d | 44 |
| apple | e | 55 |
| orange | d | 99 |
| orange | c | 33 |
| orange | d | 10 |
| banana | e | 55 |
| banana | d | 10 |
+--------+------+------+
所需输出
+--------+------+------+
| col1 | col2 | col3 |
+--------+------+------+
| apple | d | 10 |
| orange | d | 10 |
| banana | d | 10 |
+--------+------+------+
我们将检查 col2 和 col3 中 N 种不同类型的水果。
我们只想列出那些 col2 和 col3 值相同并且所有水果都存在的行
扩展解释:
你可以这样想:-
STEP 1
分离出所有不同类型的水果:-
Apple:-
+-------+------+------+
| col1 | col2 | col3 |
+-------+------+------+
| apple | d | 10 |
| apple | d | 44 |
| apple | e | 55 |
+-------+------+------+
Orange:-
+--------+------+------+
| col1 | col2 | col3 |
+--------+------+------+
| orange | d | 99 |
| orange | c | 33 |
| orange | d | 10 |
+--------+------+------+
Banana:-
+--------+------+------+
| col1 | col2 | col3 |
+--------+------+------+
| banana | e | 55 |
| banana | d | 10 |
+--------+------+------+
STEP 2:-
现在只选择那些行
AND
观察:-
“apple e 55”和“banana e 55”具有相同的 col2 和 col3 值,但未选择它,因为“orange e 55”不存在。
如果您使用临时表,请确保它应该是通用的。它应该支持N个水果。
注意:- 这不是学生的作业:D
。我用简单的话解释它,因为它是一个漫长查询的一部分,我对如何解决它的想法为零。我一直在使用创建临时表的技术,但遇到了一些问题。它不是通用的。所以,我相信这个问题可能有更好的解决方案。