Select DISTINCT wpg.ID as id1,wr.ID as id2
FROM Table1 wpg
inner join Table2 wp ON wp.WpgId = wpg.ID
inner join Table3 wr ON wr.WpId = wp.ID
我需要wpg。我明白这是怎么做的?
我需要:
1 2
2 3
1 4
得到:
1 2
2 3
答案 0 :(得分:3)
答案取决于您想要对第二列做什么。我假设你想要最小的值:
select
wpg.ID as id1,
min(wr.ID) as id2
from
Table1 wpg
inner join Table2 wp on wp.WpgId = wpg.ID
inner join Table3 wr on wr.WpId = wp.ID
group by
wpg.ID
答案 1 :(得分:2)
select wpg.ID, min(wr.ID)
FROM Table1 wpg
inner join Table2 wp ON wp.WpgId = wpg.ID
inner join Table3 wr ON wr.WpId = wp.ID
group by wpg.ID