简体   繁体   English

如何将一个表中的行合并到另一表中的另一行

[英]How to merge rows from one table to another row in another table

I want to merge rows from one table to another row in another table 我想将一个表中的行合并到另一表中的另一行

Always value of the "amount" relating to the "name" is more than 10, but split into several pieces 与“名称”相关的“金额”的总值始终大于10,但分为几部分

If the value of the "amount" column is less than 10 collect all smaller than 10 bring together the values ​​of the column "description" where has the same name 如果“金额”列的值小于10,则收集所有小于10的值,将名称相同的“描述”列的值汇总在一起

My goal is to not exist row "amount" below 10 in the new table 我的目标是在新表中不存在低于10的行“金额”

aka 又名

I want this form table - 'table' 我想要这个表格表格-'表格'

id(1) name(Name1) description(Description1) amount(5.50) id(1)名称(Name1)描述(Description1)数量(5.50)

id(2) name(Name1) description(Description2) amount(5.50) id(2)名称(Name1)描述(Description2)数量(5.50)

id(3) name(Name2) description(Description1) amount(3.50) id(3)名称(Name2)描述(Description1)数量(3.50)

id(4) name(Name2) description(Description1) amount(3.50) id(4)名称(Name2)描述(Description1)数量(3.50)

id(5) name(Name2) description(Description2) amount(3.50) id(5)名称(Name2)描述(Description2)数量(3.50)

id(6) name(Name3) description(Description3) amount(10.00) id(6)名称(Name3)描述(Description3)数量(10.00)

become in to this in a new table - 'newtable' 进入新表中的“ newtable”

Newid(1) name(Name1) description(Description1,Descripton2) amount(11.00) Newid(1)名称(Name1)描述(Description1,Descripton2)数量(11.00)

Newid(2) name(Name2) description(Description1,Description1,Descripton2) amount(10.50) Newid(2)名称(Name2)描述(Description1,Description1,Descripton2)数量(10.50)

Newid(3) name(Name3) description(Description3) amount(10.00) Newid(3)名称(Name3)描述(Description3)数量(10.00)

Any ideas? 有任何想法吗?

You can use group by , group_concat and sum for that. 您可以使用group bygroup_concatsum

Here's little sample: 这是小样本:

SELECT
`name`,
group_concat(`description`) as `description`,
sum(`amount`) as `amount` 
FROM `table_name`
GROUP BY `name`

BTW, I forgot to ask: What have you tried? 顺便说一句,我忘了问: 您尝试了什么?

Maybe something like this: 也许是这样的:

select into newtable(name,newdesc,amt)
SELECT name,
        GROUP_CONCAT(DISTINCT description
                  ORDER BY id DESC SEPARATOR ' '), sum(amount)
     FROM table
     GROUP BY name;

暂无
暂无

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

相关问题 如何从一个表中获取所有产品,并从包含多行的另一张表中合并一行? - How to fetch all the products from one table and merge one row from another table which contain multiple rows? 如何从表中删除一行,而在另一表上删除多行相关? - How to delete one row from a table and multiple rows on another that are related? 从表中选择只连接另一个表的一行的行 - SELECT rows from a table joining only one row of another table 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table 如果MySQL中的另一个表中存在行,则从一个表中删除行 - Deleting rows from one table if row exists in another table in MySQL 如何获得一个表中的所有行都暗示另一个表中的行? - How to get all rows in one table that imply to a row in another table? 如何将一行从一个表更新到另一个表? - How to update a row from one table to another? 如何从一个表中选择单行和从另一个表中选择多行? - how to select single row from one table and multiple rows from another table toghether? 如何返回一个表中其字段包含来自另一表中任何行的字符串的行? - How to return rows in one table whose field contains a string from any row in another table? MySQL:如何通过跳过源表中的空行将行从一个表复制到另一个表? - MySQL: How to copy row from one table to another with skipping empty rows in source table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM