我有
<table1>
column1 column2
1 A
2 B
2 C
<table2>
column3 column4
1 C
2 D
当我离开加入时
SELECT table1.column1,
table1.column2,
table2.column4
FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column3
我得到了
column1 column2 column4
1 A C
2 B D
2 C D
是否有可能以某种方式获得输出,
column1 column2 column4
1 A C
2 B
2 C D
我希望每次重复的table1.column1,table2.column4只应该出现一次,剩下的时间就是空白。
我尝试过各种各样的事情但是没有成功。
答案 0 :(得分:0)
尝试此查询:
select t4.column1,t4.column2,case when max2 is null
then null else t4.column4 end as column4 from
(select table1.column1, table1.column2, table2.column4,t3.* from table1
left join table2 on table1.column1 = table2.column3
left outer join
(select max2,max1 from
(select max(column2) max2,min(column2) min2,
max(column1) max1,min(column1) min1 from
(select table1.column1, table1.column2, table2.column4 from table1
left join table2 on table1.column1 = table2.column3)t1
group by column4)t2)t3
on table1.column1 = t3.max1 and table1.column2 = t3.max2)t4;
答案 1 :(得分:0)
试一试
select
new_t1.column1, new_t1.column2, if(new_t1.get_value, t2.column4,'')
from
(select
t1.*, if(t1_tmp.column2 is null, 1, 0) as get_value
from table1 t1
left join table1 t1_tmp on t1.column1 = t1_tmp.column1 and t1.column2 < t1_tmp.column2
) as new_t1
left join table2 t2 on new_t1.column1 = t2.column3