简体   繁体   English

如何在MySQL中合并多行?

[英]How to merge multiple rows in MySQL?

I ran into some problems while structuring my database, and I will ask two questions. 在构建我的数据库时遇到了一些问题,我会问两个问题。

First question: below table needs to be merged by the same IDs 第一个问题:下表需要用相同的ID合并

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║     ║       ║
 ║  0  ║       ║ 11  ║       ║
 ║  0  ║       ║     ║   6   ║
 ║  1  ║ Dave  ║     ║       ║
 ║  1  ║       ║ 12  ║       ║
 ║  1  ║       ║     ║   7   ║
 ╚═════╩═══════╩═════╩═══════╝

so it should look like this; 所以看起来应该是这样的;

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║ 11  ║   6   ║
 ║  0  ║ Dave  ║ 12  ║   7   ║
 ╚═════╩═══════╩═════╩═══════╝

NOTE: id* is not AUTO_INCREMENT 注意:id *不是AUTO_INCREMENT


Second question: You probably think that the former database structure is poor. 第二个问题:您可能认为以前的数据库结构很差。 The good thing is, I haven't created the database yet and I have been looking for a solution to add data to an existing row without removing old information, but if there is no old information, it would create a new row. 好消息是,我还没有创建数据库,我一直在寻找一种解决方案,将数据添加到现有行而不删除旧信息,但如果没有旧信​​息,它将创建一个新行。 Thanks in advance. 提前致谢。

Second question explained 第二个问题解释

Virgin table 处女表

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║     ║       ║     ║       ║
 ╚═════╩═══════╩═════╩═══════╝

some SQL statement 一些SQL语句

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║     ║       ║
 ╚═════╩═══════╩═════╩═══════╝

the same SQL statement with different parameters 具有不同参数的相同SQL语句

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║     ║       ║
 ║  1  ║ Dave  ║     ║       ║
 ╚═════╩═══════╩═════╩═══════╝

another SQL statement 另一个SQL语句

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║     ║       ║
 ║  1  ║ Dave  ║ 12  ║       ║
 ╚═════╩═══════╩═════╩═══════╝

another SQL statement 另一个SQL语句

 ╔═════╦═══════╦═════╦═══════╗
 ║ id* ║ name  ║ age ║ grade ║
 ╠═════╬═══════╬═════╬═══════╣
 ║  0  ║ John  ║     ║   6   ║
 ║  1  ║ Dave  ║ 12  ║       ║
 ╚═════╩═══════╩═════╩═══════╝

... and so on. ... 等等。

You should be able to apply an aggregate function to all the columns and then GROUP BY id : 您应该能够将聚合函数应用于所有列,然后应用GROUP BY id

select id,
  max(name) name,
  max(age) age,
  max(grade) grade
from yourtable
group by id

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

As far as the DB structure, the only issue that I see is that you are inserting multiple records for the same user. 就数据库结构而言,我看到的唯一问题是您要为同一用户插入多个记录。 You should be using an UPDATE statement to use the values instead of inserting. 您应该使用UPDATE语句来使用值而不是插入。

It sounds like you want to use the REPLACE function in MySQL ( here is a tutorial ). 听起来你想在MySQL中使用REPLACE函数( 这是一个教程 )。

So the query would be similar to this: 所以查询将类似于:

REPLACE 
  INTO yourtable (`id`, `name`, `age`, `grade`) 
  VALUES (0, 'john', 11, null);

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

You could group by id , and use any aggregate function to pick the non-null row. 您可以按id分组,并使用任何聚合函数来选择非空行。 This example uses max , but min would work too: 此示例使用max ,但min也可以使用:

select  id
,       max(name) as name
,       max(age) as age
,       max(grade) as grade
from    YourTable
group by
        id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM