简体   繁体   English

SQL查询没有给出确切的答案

[英]sql query not giving exact answer

sql query is working but last two and condition in not giving exact answer SQL查询工作,但最后两个和条件没有给出确切的答案

query: 查询:

SELECT products.*,wishlist.user_id,wishlist.p_id 
FROM products 
LEFT JOIN wishlist 
  ON products.pro_id=wishlist.p_id AND products.categ_id=2 AND wishlist.user_id=5

When you use a left join , conditions on the first table go in the where clause. 使用left join ,第一个表上的条件将进入where子句。 Conditions on the second go in the on : 在第二条件走在on

SELECT p.*, wl.user_id, wl.p_id 
FROM products p LEFT JOIN
     wishlist wl
     ON p.pro_id = wl.p_id AND wl.user_id = 5
WHERE p.categ_id = 2;

This might seem like an arcane "feature" of SQL. 这看起来像是SQL的“神秘”功能。 But it actually makes a lot of sense. 但这实际上很有意义。 The LEFT JOIN keeps all rows in the first table, even when the ON clause does no evaluate to true. LEFT JOIN所有行保留在第一个表中,即使ON子句的求值不为true。 That includes conditions on the first table. 这包括第一张桌子上的条件。 In other words, the ON clause ignores conditions on the first table. 换句话说, ON子句忽略第一个表上的条件。

Your question doesn't specify what problem you are having so I will go through how to troubleshoot this. 您的问题并未指明您遇到的问题,因此我将介绍如何解决此问题。

First, this is a left join so presumably you want everything in the products table, combined with some set of things in the left join table. 首先,这是一个左联接,因此大概您希望products表中的所有内容都与左联接表中的一些东西结合在一起。 There's a huge difference with outer joins in the join condition vs the where clause. 联接条件与where子句之间的外部联接存在巨大差异。

Let's go over what a few permutations mean. 让我们看一下一些排列的含义。 Usually you want to take a look at these first. 通常,您首先要看一看。 My recommendation is you start by changing to an inner join and see what you get. 我的建议是从开始更改为内部联接开始 ,然后看看您得到了什么。 It is usually simpler to start by troubleshooting an inner join and go to the outer join after. 通常,从对内部联接进行故障排除开始然后再转到外部联接会更简单。

So start with: 因此开始:

SELECT products.*,wishlist.user_id,wishlist.p_id 
FROM products 
JOIN wishlist 
ON products.pro_id=wishlist.p_id AND products.categ_id=2 AND wishlist.user_id=5

Make sure that the results are what you expect first. 确保结果是您首先期望的结果。 Then figure out what you want after. 然后弄清楚你想要什么。 If you want all products, with Wishlist data appended to category 2 and user id, then go back to your original query. 如果您想要所有产品,并将“愿望清单”数据附加到类别2和用户ID,然后返回到原始查询。 If you want products in category2 with user_id's Wishlist data then do this: 如果您希望类别2中的产品具有user_id的愿望清单数据,请执行以下操作:

SELECT products.*,wishlist.user_id,wishlist.p_id 
FROM products 
LEFT JOIN wishlist 
     ON products.pro_id=wishlist.p_id AND wishlist.user_id=5
where products.categ_id=2

There are several other possible permutations but unless you are clear about what answers you are looking for from the database, that's the best advice I can give. 还有其他几种可能的排列方式,但是除非您清楚从数据库中寻找什么答案,否则这是我能提供的最佳建议。

You are adding your condition in your join statement; 您正在将条件添加到join语句中; since it's a left join that means you'll be getting results from your products table whither they matched or not. 因为是左连接,这意味着无论匹配与否,您都将从产品表中获得结果。

If you want to get only results that matches your products in wishlist you'll have to change it to a where statement or use inner join instead of left join 如果只想获得与愿望清单中的产品匹配的结果,则必须将其更改为where语句,或使用内部联接而不是左联接

如果可行,请尝试以下方法:

SELECT p.*, w.user_id, w.p_id FROM products p LEFT JOIN wishlist w ON p.pro_id = w.p_id WHERE p.categ_id = 2 AND w.user_id = 5

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

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