我有table1和table2
table1
nameID column1 column2 column3
Joe 10 10 99
表2
nameID column1 cost
Joe 99 100
Joe 10 100
Joe 30 200
我的目標是讓 table1 中的 column3 與 table2 中的 column1 匹配。
IF table2有 2 條或更多記錄,即使 table1 column3 與 table2 column1 (99) 匹配并且沒有任何輸出。
如果表 2 在 column1 (99) 上只有一條記錄與 table1 column3 (99) 匹配,則預期結果
nameID column3 cost
Joe 99 100
我努力了
select t1.name, t1.column3
from table1 t1
join table2 t1 on t1.nameID = t2.nameID
where
t1.column3 = t2.column1
and t1.column1 <> t2.column1
and t1.column2 <> t2.column1
不知道如何使它作業。謝謝你。
uj5u.com熱心網友回復:
WITH CTE_X
AS (
SELECT count(*) AS countX
FROM table2 t2
)
SELECT table1.nameId
,table1.column3
FROM table1
INNER JOIN table2 ON table1.column3 = table2.column1
JOIN CTE_X ON 1 = 1
WHERE CTE_X.countX = 1
請看這個
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/421782.html
標籤:
