gpt4 book ai didi

php - 更改表合并列中具有相同值的 2 行

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:30 24 4
gpt4 key购买 nike

我有一个表,我想合并具有相同名称的两行的数据,如下所示:

id | name    | price | priced

1 | Samsung | 100 | 0

2 | Samsung | 0 | 500

我希望最终输入是:

id               | name    | price | priced

(doesn't matter) | Samsung | 100 | 500

最佳答案

试试这个:

SELECT MAX(id) as id, name,MAX(price) as price, MAX(priced) as priced
FROM TableName
GROUP BY name

插入表格:

INSERT INTO Table2 VALUES
(SELECT MAX(id) as id, name,MAX(price) as price, MAX(priced) as priced
FROM TableName
GROUP BY name)

编辑

如你所说,合并(插入新的和删除其他的)

INSERT INTO TableName VALUES
(SELECT MAX(id)+1 as id, name,MAX(price) as price, MAX(priced) as priced
FROM TableName
GROUP BY name)

--Now deleting others
DELETE T1 FROM TableName T1, TableName T2 WHERE T1.id < T2.id AND T1.name = T2.name

关于php - 更改表合并列中具有相同值的 2 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789304/

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