查询加入?包括表中的所有记录 [英] Query Join? Include all records from table

查看:52
本文介绍了查询加入?包括表中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI


我有2个表ProductType和Sales。


我正在尝试编写一个查询,它给出了产品类型的摘要和每个产品的销售。


产品.....................销售


A .. ............................ 100

B ............ ................... 50

C ..................... .......... 250

D .............................. ..0

E ................................ 20


然而,当我运行我的查询时,我得到了


产品.....................销售


A .............................. 100

B ............................... 50

C ...... ......................... 250

E ............... ................. 20

产品D缺失,因为它没有销售。但是我仍然希望它包含在Query的结果中。我尝试过左右连接,但他们仍然不会退回产品表中的所有产品?


有人可以帮忙吗?


谢谢

解决方案


HI


我有2个表ProductType和Sales。


我正在尝试编写一个查询,它给出了每个产品的产品类型和销售总结。


产品.....................销售


A .. ............................ 100

B ............ ................... 50

C ..................... .......... 250

D .............................. ..0

E ................................ 20


然而,当我运行我的查询时,我得到了


产品.....................销售


A .............................. 100

B ............................... 50

C ...... ......................... 250

E ............... ................. 20

产品D缺失,因为它没有销售。但是我仍然希望它包含在Query的结果中。我尝试过左右连接,但他们仍然不会退回产品表中的所有产品?


有人可以帮忙吗?


谢谢



你可以使用LEFT JOIN(或合适的加入)


SELECT Product.Name,Sales.Total

FROM产品

LEFT JOIN销售

ON Product.Product_ID = Sales.Product_ID


请检查以下内容:
W3school SQL LEFT JOIN


您可以使用左连接(或右连接)


SELECT Product.Name,Sales.Total
FROM Product

LEFT JOIN销售

ON Product.Product_ID = Sales.Product_ID


请检查以下内容:
W3school SQL LEFT JOIN



我是,但我不起作用。也许是因为我在查询中包含了一个WHERE stmt?



我但是它不起作用。也许是因为我在查询中包含了一个WHERE stmt?



这是可能的。看看你是否可以先使用WHERE语句使其工作。


HI

I have 2 tables ProductType and Sales.

I am trying write a query which gives me a summary of Product Types and Sales for each Product.

Product.....................Sales

A..............................100
B...............................50
C...............................250
D................................0
E................................20

However when I run my query I get

Product.....................Sales

A..............................100
B...............................50
C...............................250
E................................20

Product D is missing because it has no Sales. However I still want it included in results of Query. I have tried left and right joins but they still wont return all the products in the products table?

Can anyone help?

Thanks

解决方案

HI

I have 2 tables ProductType and Sales.

I am trying write a query which gives me a summary of Product Types and Sales for each Product.

Product.....................Sales

A..............................100
B...............................50
C...............................250
D................................0
E................................20

However when I run my query I get

Product.....................Sales

A..............................100
B...............................50
C...............................250
E................................20

Product D is missing because it has no Sales. However I still want it included in results of Query. I have tried left and right joins but they still wont return all the products in the products table?

Can anyone help?

Thanks

You could use a LEFT JOIN (or RIGHT JOIN)

SELECT Product.Name, Sales.Total
FROM Product
LEFT JOIN Sales
ON Product.Product_ID=Sales.Product_ID

check the following :
W3school SQL LEFT JOIN


You could use a LEFT JOIN (or RIGHT JOIN)

SELECT Product.Name, Sales.Total
FROM Product
LEFT JOIN Sales
ON Product.Product_ID=Sales.Product_ID

check the following :
W3school SQL LEFT JOIN

I am but it doesnt work. Maybe because I have a WHERE stmt included in the Query?


I am but it doesnt work. Maybe because I have a WHERE stmt included in the Query?

it''s possible. See if you can make it work first without the WHERE statement first.


这篇关于查询加入?包括表中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆