简体   繁体   English

SQL NOT查询返回错误结果

[英]SQL NOT query returns wrong results

I have 2 SQL tables. 我有2个SQL表。 users contains users and registrants contains all the registrations done for an event. 用户包含用户, 注册人包含事件的所有注册。 Given that I have the id of the event - I am successfully printing out those users that are registered for a specific event. 有了事件的ID,我就可以成功打印出为特定事件注册的用户。 However I want to print all the users that are NOT registered for the event and here I fail at producing the correct result. 但是,我想打印所有未注册该事件的用户,并且在此我无法产生正确的结果。 My query is as follows: 我的查询如下:

SELECT users.fName, users.lName, users.uid
FROM users, registrants
WHERE registrants.tourId = '$id' AND NOT registrants.pId = users.uid

The result of this is an array of 33 users printed out, although I have only 12, 3 of which are already registered for the event. 结果是输出了33个用户数组,尽管我只有12个,其中3个已经注册了该事件。 Is the SELECT command wrong or I will need to dig into the php code, although I am pretty sure all the work there is correct. SELECT命令是错误的,还是我需要深入研究php代码,尽管我可以肯定那里的所有工作都是正确的。

By specifying no JOIN syntax, but instead specifying a kind of inequality join in the WHERE clause, you are effectively asking SQL to find all combinations of users and registrants, with the additional filter criteria in the WHERE clause, hence the large number of rows. 通过不指定JOIN语法,而是在WHERE子句中指定一种不等式JOIN ,可以有效地要求SQL查找用户和注册者的所有组合,并在WHERE子句中具有附加的筛选条件,因此需要大量的行。

I believe the query you are looking for is more like this: 我相信您要查找的查询更像这样:

SELECT users.fName, users.lName, users.uid
FROM users
WHERE users.uid NOT IN (
   SELECT registrants.pId 
   FROM registrants
   WHERE registrants.tourId = '$id');

"Find all users, except those registered for tourId = '$id'" “查找所有用户,但为tourId ='$ id'注册的用户除外”

Assuming that "registrants.pId" is the foreign key to the table "users", this request will give you every combination of registrants and users where the registrant and user are not related. 假设“ registrants.pId”是表“ users”的外键,则此请求将为您提供与注册人和用户无关的注册人和用户的每种组合。

The way I see you could achieve the intended result is to select users using a subquery to exclude those who are regitered to the event. 我认为您可以达到预期结果的方式是使用子查询来选择用户,以排除那些对该事件感兴趣的用户。 Which would look something like : 看起来像这样:

$query = "SELECT users.fName, users.lName, users.uid FROM users WHERE users.uid not in (SELECT users.uid FROM users, registrants WHERE registrants.tourId = '$id' AND registrants.pId = users.uid)"

there might be a simpler way to do this, though I do not see it right now. 可能有一种更简单的方法来执行此操作,尽管我现在看不到它。

Have you tried ".... AND registrants.pld <> users.uid"; 您是否尝试过“ .... AND registrants.pld <> users.uid”; ?

or 要么

".... AND registrants.pld != users.uid"; “ .... AND registrants.pld!= users.uid”;

You need to use an anti-join, that is, an outer join where you want to return from the dependent side of the join values where the independent side of the join is null. 您需要使用一个反联接,即外部联接,您要从联接值的从属端返回,而联接的独立端为null。

In your case that looks like this: 在您的情况下,如下所示:

SELECT users.fName, users.lName, users.uid
FROM users
LEFT OUTER JOIN registrants
ON users.uid = registrants.pId
AND registrants.tourId = '$id'
WHERE registrants.pId IS NULL

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

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