将 SQL 查找表与数据表连接起来
我有一个查找表,表示带有字段 CityId、CityName 的城市
CityId CityName
1 New York
2 San Francisco
3 Chicago
我有一个订单表,其中包含以下字段: CityId、CustId、CompletedOrders、PendingOrders
CityId CustId CompletedOrders PendingOrders
1 123 100 50
2 123 75 20
我想要一个表/报告,列出所有城市中给定客户的订单详细信息,即结果我需要的是:
CityId CityName CustId CompletedOrders PendingOrders
1 New York 123 100 50
2 San Francisco 123 75 20
3 Chicago 123 0 0
如何做到这一点?
I have a lookup table say cities with fields CityId, CityName
CityId CityName
1 New York
2 San Francisco
3 Chicago
I have an orders table which has fields: CityId, CustId, CompletedOrders, PendingOrders
CityId CustId CompletedOrders PendingOrders
1 123 100 50
2 123 75 20
I want a table/report that lists orders details of a given customer in all cities, i.e. the result I need is:
CityId CityName CustId CompletedOrders PendingOrders
1 New York 123 100 50
2 San Francisco 123 75 20
3 Chicago 123 0 0
How to do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将返回您想要的所有行,但对于
details
中不存在的行,它将返回NULL
值,因此您将得到:
0
相反取决于您的数据库。对于 MySQL,使用IFNULL
,对于 Oracle,使用NVL
。This will return all the rows that you want, but for the rows that don't exist in
details
it will returnNULL
values, so you would get:The solution to get
0
instead depends on your database. With MySQL useIFNULL
, with Oracle useNVL
.试试这个
try this