简体   繁体   English

使用MySQL使用LEFT JOIN返回结果,其中一个表不包含记录

[英]Using MySQL to return results using LEFT JOIN where one of the tables does not contain a record

This is a bit of a random question, and I've been doing this using multiple queries in MySQL and a small bit of PHP. 这是一个随机的问题,我一直在使用MySQL和一小部分PHP中的多个查询。

I have a MySQL query: 我有一个MySQL查询:

SELECT * 
FROM (reviews LEFT JOIN orders ON reviews.orderid = orders.orderid) 
LEFT JOIN customers ON orders.custid = customers.id 
WHERE customers.email = '$email'

which returns all product reviews from a specific customer. 返回特定客户的所有产品评论。

I would like to know structure a separate query which would return all of the customer's orders which are not yet reviewed. 我想知道一个单独的查询结构,该查询将返回尚未审查的所有客户订单。 That is, where there is no record in the reviews table for a particular orderid. 也就是说,评论表中没有特定orderid的记录。

So... 所以...

SELECT * 
FROM orders LEFT JOIN customers ON orders.custid = customers.id 
WHERE customers.email = '$email'

... which will return all customer orders, but I then want to perhaps use another WHERE clause and a LEFT JOIN so that now only orderid's with no corresponding review records are returned. ...它将返回所有客户订单,但我想要使用另一个WHERE子句和LEFT JOIN,以便现在只返回没有相应审核记录的orderid。 I've been trying to find something that will do this for some time now without any luck. 我一直试图找到一些能够做到这一点的东西,现在没有任何运气。 Nothing seems to do the job. 似乎什么都没做。

As stated above, I have managed to do this using a combination of PHP/MySQL, however, it isn't very efficient, so I wondered if anyone had any suggestions? 如上所述,我已经设法使用PHP / MySQL的组合来做到这一点,但是,它效率不高,所以我想知道是否有人有任何建议?

Thanks in advance. 提前致谢。

Ryan 瑞安

SELECT * 
FROM customers 
  JOIN orders 
    ON orders.custid = customers.id 
WHERE customers.email = '$email' 
  AND orders.orderid NOT IN 
        ( SELECT orderid FROM reviews )

... maybe ... 也许

did you try this ? 你试过这个吗?

SELECT orders.* FROM orders 
LEFT JOIN customers ON orders.custid = customers.id
LEFT JOIN reviews ON reviews.orderid=orders.id
 WHERE customers.email = '$email' AND reviews.orderid IS NULL

this should return all unreviewed orders for the customer of email $email 这应该返回电子邮件$ email客户的所有未审查的订单

try this: 尝试这个:

SELECT * 
FROM  orders a LEFT JOIN customers b ON a.custid = b.id 
WHERE b.email = '$email' AND
      a.orderid NOT IN (SELECT orderid FROM reviews)

You can LEFT JOIN your customers/orders onto reviews and select rows with no corresponding review : 您可以将您的客户/订单LEFT JOIN reviews并选择没有相应review行:

SELECT * 
FROM orders LEFT JOIN customers ON orders.custid = customers.id 
LEFT JOIN reviews ON reviews.orderid = orders.orderid
WHERE customers.email = '$email'
 AND reviews.orderid IS NULL

This will work but it could be slow. 这可行,但可能会很慢。

SELECT * 
FROM orders 
LEFT JOIN customers 
  ON orders.custid = customers.id 
WHERE customers.email = '$email'
AND orders.orderid NOT IN (SELECT orderid FROM reviews)

This would be faster. 这会更快。

SELECT orders.orderid 
FROM orders 
LEFT JOIN customers 
  ON orders.custid = customers.id 
LEFT JOIN reviews
  ON reviews.orderid = orders.orderid
WHERE customers.email = '$email'
GROUP BY orders.orderid
HAVING count(reviews) = 0

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

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