![](/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.