簡體   English   中英

SQL 加入不同的同表 select

[英]SQL join with the same table with different select

我有一個包含 col1-col10 列的表 tablename。 並非每一行都填充了 col4 ,但每一行都填充了 col1、col2、col3。 我想獲取 col4 滿足條件時的所有 {col1, col2, col3} 元組,然后從表名中獲取與元組 {col1, col2, col3} 匹配的所有行。

我不確定我是否應該使用 inner join 或 left join 或其他東西? (我認為內部連接和左連接都應該給我相同的結果)下面的查詢給我一個語法錯誤“輸入不匹配”。 編寫此查詢的正確方法是什么?

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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM