gpt4 book ai didi

PHP 删除第二条记录组名称

转载 作者:行者123 更新时间:2023-11-29 06:37:23 25 4
gpt4 key购买 nike

任何人都知道如何使用相同的组对记录进行分组,但下一行的组名称在 PHP/MySQL 中为空,例如:

数据库表上的记录:

| id | group_title | item     | qty | price
| 1 | G-01 | Mouse | 1 | $5
| 2 | G-01 | Keyboard | 1 | $12
| 3 | G-01 | Keyboard | 1 | $12
| 4 | G-01 | Cable | 1 | $1
| 5 | G-02 | Headset | 1 | $20

预期结果:

| Group | Item Name | Qty | Price/qty | Price Total/item | Price Total/group
| G-01 | Mouse | 1 | $5 | $5 | $30
| | Keyboard | 2 | $12 | $24 |
| | Cable | 1 | $1 | $1 |
| G-02 | Headset | 1 | $20 | $20 | $20

我已经尝试了很多天了,但还是没有解决。提前致谢。

最佳答案

您可以使用简单的 IFuservar 来完成此操作,如下查询:

SELECT 
IF(g.gname = @myg, '', @myg:= gname) AS gname,
g.mytext
FROM grouptab g
CROSS JOIN ( SELECT @myg:='') AS init
ORDER BY g.gname;

示例

MariaDB [test]> select * from grouptab;
+----+-------+---------+
| id | gname | mytext |
+----+-------+---------+
| 1 | G01 | Name 1 |
| 2 | G01 | Name 2 |
| 3 | G02 | Name 88 |
| 4 | G02 | Name 99 |
| 5 | G01 | Name 4 |
+----+-------+---------+
5 rows in set (0.000 sec)

MariaDB [test]> SELECT
-> IF(g.gname = @myg, '', @myg:= gname) AS gname,
-> g.mytext
-> FROM grouptab g
-> CROSS JOIN ( SELECT @myg:='') AS init
-> ORDER BY g.gname;
+-------+---------+
| gname | mytext |
+-------+---------+
| G01 | Name 1 |
| | Name 2 |
| | Name 4 |
| G02 | Name 88 |
| | Name 99 |
+-------+---------+
5 rows in set (0.000 sec)

MariaDB [test]>

关于PHP 删除第二条记录组名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53186120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com