[英]SQL Select rows based on distinct column value
I'm trying to return postcode data for every country in our order table, this should be a straightforward query but I can't quite get it to work.我正在尝试为我们的订单表中的每个国家/地区返回邮政编码数据,这应该是一个简单的查询,但我无法让它正常工作。 The query executes fine but every row of the result is just:
查询执行良好,但结果的每一行只是:
POSTCODE
| POSTCODE
| COUNTRY
| COUNTRY
| id < the country id My query is as follows:我的查询如下:
SELECT 'POSTCODE' as billing_postcode, 'COUNTRY' as Title, billing_country_id
FROM [DB].[dbo].[Order]
INNER JOIN [DB].[dbo].[Country]
ON country_id = billing_country_id
GROUP BY billing_country_id
Below is an example of the output that I'm getting: -下面是我得到的输出示例:-
Why is it not returning the postcode + country?为什么不返回邮政编码+国家? Can anyone see what I've done wrong here?
谁能看到我在这里做错了什么?
Thanks谢谢
edit*编辑*
Basic Order table structure:基本订单表结构:
OrderId |
订单编号 | billing_postcode |
billing_postcode | billing_country_id
billing_country_id
Basic Country table structure:基本国家表结构:
country_id |
country_id | Title
标题
You've selected the string PostCode
and Country
not the actual columns.您选择了字符串
PostCode
和Country
而不是实际的列。
SELECT POSTCODE as 'billing_postcode', COUNTRY as 'Title', billing_country_id
FROM [DB].[dbo].[Order]
INNER JOIN [DB].[dbo].[Country]
ON country_id = billing_country_id
GROUP BY billing_country_id
From your edit it seems that you need to switch the columns/alias around:从您的编辑看来,您似乎需要切换列/别名:
SELECT billing_postcode AS 'POSTCODE', Title as 'Country', billing_country_id
FROM [DB].[dbo].[Order]
INNER JOIN [DB].[dbo].[Country]
ON country_id = billing_country_id
Do like this.这样做。 Remove apostrophes from column name and instead use it in alias part
从列名中删除撇号,并在别名部分使用它
SELECT POSTCODE as 'billing_postcode', COUNTRY as 'Title', billing_country_id
FROM [DB].[dbo].[Order]
INNER JOIN [DB].[dbo].[Country]
ON country_id = billing_country_id
GROUP BY billing_country_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.