java - MYSQL 中特定列中的重复项显示为空

标签 java mysql sql select subquery

我在 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.

最佳答案

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

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

关于java - MYSQL 中特定列中的重复项显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38735519/

相关文章:

php - 我可以在谷歌应用引擎中运行 php mysql 吗

sql - 当您执行 `SELECT *` 时,SQL Server 如何确定列的顺序?

java - 如何在java中的azure中的父目录下添加子目录?

java - 匹配以 <i 开头、后跟 'mg' 且以 '>' 结尾的字符串的正则表达式

java - 绘制分层形状

python - 运行 makemigrations django 后出现 mysql 格式错误的数据包错误

Python:生成可用于 MySQL 的日期时间字符串

mysql - 基于 SQL MIN() 选择额外字段

mysql - 用于将日期分组为括号开始和结束组的 Sql 查询

java - 将 JPanel 上绘制的图片保存在文件中 [java]