简体   繁体   English

Sql Left join什么都不返回

[英]Sql Left join returns nothing

I want to join table of products and table of orders in a way that all products are listed and price of all ordered product by current user are shown (However some of those prices are 0 because there is no order submitted for that product). 我希望以所有产品列出的方式加入产品表和订单表,并显示当前用户的所有订购产品的价格(但是这些价格中的一些是0,因为没有为该产品提交订单)。

As the user confirms the list, An invoiceId is produced for all products in this list. 当用户确认列表时,将为此列表中的所有产品生成一个invoiceId。 So the invoiceId is null in this step yet (which is my question). 所以在这一步中invoiceId为空(这是我的问题)。

There are many products in first table but why the output is empty? 第一个表中有很多产品,但为什么输出是空的?

Table products: 表产品:

id | id | price 价钱

Table orders: 表订单:

id | id | ProductId | ProductId | member | 会员| invoiceId invoiceId

(I added orders.member is null regarding to this question but no chance yet): (我添加了orders.member is null对于这个问题 orders.member is null ,但没有机会):

Select * from products left join orders 
on products.id=orders.productId 
where (orders.member='"&request.cookies("member")&"' or  orders.member is null) 
and orders.invoiceId is null

Edit: My Guess: There are also orders of other members. 编辑:我的猜测:还有其他成员的订单。 Does they violate current where condition ? 他们是否违反了当前条件

Your condition is on the second table. 你的病情在第二张桌子上。 Hence, it should go in the on clause: 因此,它应该在on子句中:

Select *
from products p left join
     orders o
    on p.id = o.productId and
       o.member='"&request.cookies("member")&"'
where o.invoiceId is null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM