在这样的表格中:
code code1 code2 code3 code4
FILA841201 123456 481201 654987 NULL
NULL 123456 481201 1234 NULL
NULL 123456 481201 789014 324324
我只需要一行,今天就使用这些查询
CREATE TABLE tmp2(SELECT max(code)as code,code1,max(code2)as code2,max(code3)as code3,max(code4)as code4 FROM tmp1 WHERE code1 IS NOT NULL group by code1); < / p>
CREATE TABLE tmp3(SELECT max(code)as code,max(code1)as code1,code2,max(code3)as code3,max(code4)as code4 FROM tmp1 WHERE code2 IS NOT NULL group by code2); < / p>
ecc ...循环使用所有列
我的问题是,如果列中有不同的值,我必须让位给第一列填充的行。
在这个例子中,code3我必须得出“654987”而不是最大值(789014)。
我应该得到的结果
code code1 code2 code3 code4
FILA841201 123456 481201 654987 324324
感谢您的关注
答案 0 :(得分:0)
假设您有一个指定行排序的列,您可以使用相关子查询执行此操作:
select (select code from tmp1 where code is not null order by id limit 1) as code,
(select code1 from tmp1 where code1 is not null order by id limit 1) as code1,
(select code2 from tmp1 where code2 is not null order by id limit 1) as code2,
(select code3 from tmp1 where code3 is not null order by id limit 1) as code3,
(select code4 from tmp1 where code4 is not null order by id limit 1) as code4