SQL 加入相同的 table 与不同的 select
SQL join with the same table with different select
我有一个 table table 名称,列为 col1-col10。 并非每一行都填充了 col4,但每一行都填充了 col1、col2、col3。
当 col4 满足条件时,我想获取所有 {col1, col2, col3} 元组,然后从 tablename.
获取与元组 {col1, col2, col3} 匹配的所有行
我不确定我是否应该使用内部联接或左联接或其他方式? (我认为 inner 和 left join 应该给我相同的结果)
下面的查询给了我一个语法错误“不匹配的输入 AS”。编写此查询的正确方法是什么?
select col1, col2, col3
from tablename
where col4 >= 1000 AS A
INNER JOIN
(select *
FROM tablename) AS B
ON A.col1 = B.col1 AND A.col2 = B.col2 A.col3 = B.col3
您可以使用 exists
:
select t.*
from mytable t
where exists (
select 1
from mytable t1
where
t1.col1 = t.col1
and t1.col2 = t.col2
and t1.col3 = t.col3
and t1.col4 >= 1000
)
我有一个 table table 名称,列为 col1-col10。 并非每一行都填充了 col4,但每一行都填充了 col1、col2、col3。 当 col4 满足条件时,我想获取所有 {col1, col2, col3} 元组,然后从 tablename.
获取与元组 {col1, col2, col3} 匹配的所有行我不确定我是否应该使用内部联接或左联接或其他方式? (我认为 inner 和 left join 应该给我相同的结果) 下面的查询给了我一个语法错误“不匹配的输入 AS”。编写此查询的正确方法是什么?
select col1, col2, col3
from tablename
where col4 >= 1000 AS A
INNER JOIN
(select *
FROM tablename) AS B
ON A.col1 = B.col1 AND A.col2 = B.col2 A.col3 = B.col3
您可以使用 exists
:
select t.*
from mytable t
where exists (
select 1
from mytable t1
where
t1.col1 = t.col1
and t1.col2 = t.col2
and t1.col3 = t.col3
and t1.col4 >= 1000
)