[英]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 by
, group_concat
和sum
。
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.