【问题标题】:Show as empty for Duplicates in Particular column in MYSQL在 MYSQL 的特定列中显示为空
【发布时间】:2016-08-03 05:58:35
【问题描述】:

我在 MySQL 中有一个名为 Bills 的表。

Bill         item          totalprice

BILL_1       Fossil Watch  9000
BILL_1       Fastrack      9000
BILL_1       Fastrack      9000
BILL_2       Woodlands     7000
BILL_2       Woodlands     7000
BILL_3       Denim Shirt   9000
BILL_3       Levis Jean    9000

SELECT 语句应返回如下所示

Bill         item          totalprice

BILL_1       Fossil Watch  9000
BILL_1       Fastrack      -
BILL_1       Fastrack      -
BILL_2       Woodlands     7000
BILL_2       Woodlands     -
BILL_3       Denim Shirt   9000
BILL_3       Levis Jean    -
The totalprice column is total bill amount.

【问题讨论】:

  • Bill 是主键列吗?
  • 是否有任何类型的 id 列或其他列可用于对交易进行排序?
  • 嗨@Grace 它不是主列
  • @Viki Cullen:这五条记录是否意味着它们在同一张账单中?
  • 抱歉,我现在更新了表格@Grace

标签: java mysql sql select subquery


【解决方案1】:

不确定这是否是您真正想要的,但请尝试一下:

select t.Bill, t.item, t.totalPrice
from (
    select 
        b.Bill, b.item
        ,if(@price <> b.totalPrice or @price is null, b.totalPrice, '-') as totalPrice
        ,@price := b.totalPrice as dummy
    from (select * from Bills order by totalPrice desc, item desc) b
    cross join (select @price := null) t
) t

Demo Here

已编辑:

select t.Bill, t.item, t.totalPrice
from (
    select 
        b.Bill, b.item
        ,if(@price <> b.totalPrice or @price is null, b.totalPrice, '-') as totalPrice
        ,@price := b.totalPrice as dummy
    from (select * from Bills order by Bill, totalPrice desc) b
    cross join (select @price := null) t
) t

New Demo

【讨论】:

  • 我现在已经编辑了我的问题,结果应该与Bill栏相对应
猜你喜欢
  • 2016-10-26
  • 2022-01-24
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多