将列中的重复记录替换为null

时间:2016-08-02 06:39:06

标签: mysql

我需要一个mysql查询以获得以下输出,请帮助

交易表

 id   name    customer  product  amount    ref_no

  1  raja     xyz       abc      100      q123

  2  raja     xyz       def       100     q123

  3  raja     xyz       ddd       200     q123

  4  rajesh   xyz       abc       100     q124

  5  rajesh   xyz       def       200     q124

输出

name  customer  product  amount
raja   xyz       abc      100
                 def      100 
                 ddd      200
rajesh xyz       abc      100
                 def      200

提前致谢。

1 个答案:

答案 0 :(得分:0)

这一个:

set @cnt = 0;
select
case when check_cnt is null then NULL else name end as `name`,
case when check_cnt is null then NULL else customer end as customer,product,amount,ref_no from
(select  rcount,name,customer,product,amount,ref_no,
case when rcount!=1 then null else rcount end as check_cnt
 from
(select @cnt:=if(@cnt=1,customer,ROW_COUNT()+1)as rcount,
name,customer,product,amount,ref_no from `transaction`) as a) as a

结果:

enter image description here

enter image description here

与上述示例输出相同的新结果:

这是最后一个:

select
case when a.min_id is null then NULL else b.name end as `name`,
case when a.min_id is null then NULL else customer end  as `customer`
,product,amount,ref_no from
(select min(id) as min_id,count(1),name from `transaction` group by name) as a
right JOIN
(select id, name,customer,product,amount,ref_no from `transaction`) as b
on a.min_id = b.id

结果:

enter image description here