基于MySQL中的一个数字列检索排序后的数据
我在MySQL数据库中有一个类似表的附加图像 我正在尝试检索基于总和(运费)列的排序数据。为此,我使用了下面的查询 选择国家/地区 来自CountryDetails 按装运国分组 按金额(运费)ASC订购 当我运行这个时,我得到如下结果 如果我运行下面的查询,我将得到如下结果。很好 选择ShipCountry、ShipCity 来自CountryDetails 按ShipCountry、ShipCity分组 订单金额(运费),ShipCity ASC 而不是这个,我需要一个结果如下。按顺序子句和(运费)应只考虑船运。它不应该同时考虑ShipCountry和ShipCity。我的预期结果是 如何通过MySQL查询实现这个结果 在SQL中,我们可以实现如下查询 选择ShipCountry、按ShipCountry从Countrydetails组中选择ShipCity、按总和(总和(运费))选择ShipCity订单(按ShipCountry划分)、按ShipCity Asc选择ShipCity。基于MySQL中的一个数字列检索排序后的数据,mysql,mysql-workbench,mysql-error-1064,Mysql,Mysql Workbench,Mysql Error 1064,我在MySQL数据库中有一个类似表的附加图像 我正在尝试检索基于总和(运费)列的排序数据。为此,我使用了下面的查询 选择国家/地区 来自CountryDetails 按装运国分组 按金额(运费)ASC订购 当我运行这个时,我得到如下结果 如果我运行下面的查询,我将得到如下结果。很好 选择ShipCountry、ShipCity 来自CountryDetails 按ShipCountry、ShipCity分组 订单金额(运费),ShipCity ASC 而不是这个,我需要一个结果如下。按顺序
在MySQL中,我们需要类似的查询。您可以
按发货国分组
和按城市分组
如下
SELECT
ShipCountry,
GROUP_CONCAT(ShipCity ORDER BY ShipCity ASC) AS cities,
SUM(freight) AS total_country_freight
FROM
Countrydetails
GROUP BY
ShipCountry
ORDER BY
total_country_freight ASC
这将输出
阿根廷|布宜诺斯艾利斯
西班牙|波特兰
挪威|巴特,斯塔文
意大利|阿尔伯克基
葡萄牙|葡京
波兰|埃尔金、西雅图、瓦拉瓦拉、华沙
显示时,您可以用逗号拆分字符串并打印预期输出。您可以
按发货国分组
和按城市分组
如下
SELECT
ShipCountry,
GROUP_CONCAT(ShipCity ORDER BY ShipCity ASC) AS cities,
SUM(freight) AS total_country_freight
FROM
Countrydetails
GROUP BY
ShipCountry
ORDER BY
total_country_freight ASC
这将输出
阿根廷|布宜诺斯艾利斯
西班牙|波特兰
挪威|巴特,斯塔文
意大利|阿尔伯克基
葡萄牙|葡京
波兰|埃尔金、西雅图、瓦拉瓦拉、华沙
显示时,您可以使用逗号拆分字符串并打印预期输出。尝试以下操作:
SELECT t1.ShipCountry, t1.ShipCity, t2.countrysum FROM CountryDetails t1
join ( select ShipCountry, SUM(freight) countrysum from CountryDetails
group by ShipCountry )
as t2 on t1.ShipCountry = t2.ShipCountry
GROUP BY ShipCountry, ShipCity
ORDER BY countrysum ASC ;
它包括一个子查询,但应为每个国家/城市对生成单独的行。尝试以下操作:
SELECT t1.ShipCountry, t1.ShipCity, t2.countrysum FROM CountryDetails t1
join ( select ShipCountry, SUM(freight) countrysum from CountryDetails
group by ShipCountry )
as t2 on t1.ShipCountry = t2.ShipCountry
GROUP BY ShipCountry, ShipCity
ORDER BY countrysum ASC ;
它包括一个子查询,但应为每个国家/城市对生成单独的行。是。上面的查询产生了我期望的结果。谢谢你的帮助。上面的查询产生了我期望的结果。谢谢你的帮助。