How can two rows of MySQL with the same Id be merged into one row and multiple columns?

CSDN Q & A 2022-02-13 06:18:59 阅读数:396

rows mysql id merged row

I have multiple lines of data
id name amt
1 506 100
1 505 100
2 308 20
2 606 20
id There's a lot of , First of all name The original name, The second is name The new name,amt There will be no change , The effect you want to achieve is
id primary name new name amt
1 506 505 100
2 308 606 20




Take the answer :

Perform associated query on its own table .
select d a.id,a.name ' primary name',b.name1 ' new name',a.amt from surface a, surface b where a.id=b.id and a.name<>b.name group by a.id;

doubt : What is the only key in the table , There is no unique column .

If help , Please accept . Click on the top right corner of my answer 【 Adopt 】 Button .



Other answers 2:

You're wrong . You have a field in front of this id,name,amt. Your needs now become id,name,name2,amt. Don't you need to add a field ?


Other answers 3:

Lack of conditions , How to distinguish the original name New name

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130618571013.html