Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 连接两个表并将文本值合并到一列中_Mysql_Sql - Fatal编程技术网

Mysql 连接两个表并将文本值合并到一列中

Mysql 连接两个表并将文本值合并到一列中,mysql,sql,Mysql,Sql,我有以下两个表,您也可以在sqlfiddle中找到它们: 注意:每个产品都明确分配给一个采购渠道 现在,我想做一个查询,结果如下: Sales_Date Product Channel Sales_Quantity 2017-05-23 Product A Online_Local_Supplier 400 2018-09-10 Product A Store_Local_Supplier 200 201

我有以下两个表,您也可以在
sqlfiddle
中找到它们:

注意:每个产品都明确分配给一个采购渠道


现在,我想做一个查询,结果如下:

Sales_Date   Product      Channel                   Sales_Quantity
2017-05-23   Product A    Online_Local_Supplier     400 
2018-09-10   Product A    Store_Local_Supplier      200
2018-12-14   Product B    Store_Reseller            600
2019-01-03   Product B    Store_Reseller            650
:            :            :                         :
:            :            :                         :
:            :            :                         :
如您所见,我想将
销售渠道
采购渠道
合并到一列中
因此,我设置了以下查询:

SELECT 
s.Sales_Date, 
s.Product, 
(Case sales_channel
 When "Online" Then "Online"
 When "Store" then "Store"
 When "TradeFair" then "Traidfair"
 ELSE "NoSalesChannel"
 END) AS Channel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product
GROUP BY 1,2;

此查询正确地插入了
销售渠道
,但我必须如何更改它,才能将
采购渠道
添加到它中,如我所需的结果所示?

请尝试
不同的

SELECT distinct
s.Sales_Date, 
s.Product, 
concat(s.Sales_Channel, '_', p.Purchasing_Channel) as Chanel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product
尝试
concat
group by

SELECT 
s.Sales_Date, 
s.Product, 
concat(s.Sales_Channel, '_', p.Purchasing_Channel) as Chanel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product
group by s.Sales_Date, 
s.Product,
concat(s.Sales_Channel, '_', p.Purchasing_Channel),
s.Sales_Quantity
这将给您一个结果:

Sales_Date  | Product   | Chanel                 | Sales_Quantity

2017-05-23    Product A   Online_Local_Supplier    400
2018-09-10    Product A   Store_Local_Supplier     200
2018-12-14    Product B   Store_Reseller           600
2019-01-03    Product B   Store_Reseller           700
2019-02-15    Product B   Online_Reseller          650
2019-03-20    Product A   Online_Local_Supplier    380
2019-08-25    Product C   TradeFair_Foreign_Import 120
2019-09-16    Product C   Online_Foreign_Import    470
2019-09-16    Product A   Store_Local_Supplier     920
2019-10-20    Product B   TraidFair_Reseller       860
另外,请检查jarlh给您的所有评论


在似乎没有涉及p列的情况下,为什么要加入采购?您通常会将
选择的列与
选择的列进行分组,但设置函数的参数除外。在不涉及设置函数的情况下,为什么要分组?非常感谢这个全面的答案。
Sales_Date  | Product   | Chanel                 | Sales_Quantity

2017-05-23    Product A   Online_Local_Supplier    400
2018-09-10    Product A   Store_Local_Supplier     200
2018-12-14    Product B   Store_Reseller           600
2019-01-03    Product B   Store_Reseller           700
2019-02-15    Product B   Online_Reseller          650
2019-03-20    Product A   Online_Local_Supplier    380
2019-08-25    Product C   TradeFair_Foreign_Import 120
2019-09-16    Product C   Online_Foreign_Import    470
2019-09-16    Product A   Store_Local_Supplier     920
2019-10-20    Product B   TraidFair_Reseller       860