【发布时间】:2017-05-27 04:20:02
【问题描述】:
我的查询如下
select a.col1,a.col2,b.col1,b.col2 from table1 a, table2 b
where a.col3=value and b.col2 in (select col from table1 where col3=val)
这是给出重复值 例如。
得到的结果
-----------------------
S.No| Name| Value| Code
------------------------
1| Delhi| capital| 100
------------------------
2 |Mumbai| city | 101
------------------------
1 |Delhi| city| 101
------------------------
2 |Mumbai| capital |100
------------------------
预期结果
-----------------------
S.No| Name| Value| Code
------------------------
1 |Delhi| capital |100
----------------------
2 |Mumbai| city |101
----------------------
我试过 Group by 它不起作用。怎么解决?
样本数据
表 1
------------------------
S.NO| Name |Type |Value
1| Delhi |BIGCITY| Capital
-------------------------
2| Mumbai| BIGCITY| City
------------------------
表 2
---------------
Value |Code
---------------
Capital |100
---------------
City |101
--------------
select a.sno,a.name,b.value,b.code from table1 a, table2 b
where a.type=BIGCITY and b.col2 in (select value from table1 where col3=BIGCITY)
【问题讨论】:
-
您能否提供示例数据,即您要查询的表中的数据?
-
请在此处描述您真正想要实现的目标。仅从查询来看,还不是很清楚。另外,请提供输入表,以便可以复制示例。
-
如果您使用“现代”风格的连接(它们只是 SQL 的标准部分已有 25 年了)并表达了实际的 连接条件表
a和b,你不会有笛卡尔连接。 -
@Damien_The_Unbeliever 我的想法完全正确,但问题处于太破碎的状态,无法真正尝试有意义的查询。
-
对于未来的问题,您可能想阅读:stackoverflow.com/help/formatting
标签: sql cartesian-product where-in