![](/img/trans.png)
[英]How to fetch all the products from one table and merge one row from another table which contain multiple rows?
[英]How to merge rows from one table to another row in another table
我想将一个表中的行合并到另一表中的另一行
与“名称”相关的“金额”的总值始终大于10,但分为几部分
如果“金额”列的值小于10,则收集所有小于10的值,将名称相同的“描述”列的值汇总在一起
我的目标是在新表中不存在低于10的行“金额”
又名
我想要这个表格表格-'表格'
id(1)名称(Name1)描述(Description1)数量(5.50)
id(2)名称(Name1)描述(Description2)数量(5.50)
id(3)名称(Name2)描述(Description1)数量(3.50)
id(4)名称(Name2)描述(Description1)数量(3.50)
id(5)名称(Name2)描述(Description2)数量(3.50)
id(6)名称(Name3)描述(Description3)数量(10.00)
进入新表中的“ newtable”
Newid(1)名称(Name1)描述(Description1,Descripton2)数量(11.00)
Newid(2)名称(Name2)描述(Description1,Description1,Descripton2)数量(10.50)
Newid(3)名称(Name3)描述(Description3)数量(10.00)
有任何想法吗?
您可以使用group by
, group_concat
和sum
。
这是小样本:
SELECT
`name`,
group_concat(`description`) as `description`,
sum(`amount`) as `amount`
FROM `table_name`
GROUP BY `name`
顺便说一句,我忘了问: 您尝试了什么?
也许是这样的:
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.