简体   繁体   English

一个针对三个表的MySQL查询:A中有多少不在B或C中?

[英]A MySQL query addressing three tables: How many from A are not in B or C?

I have a problem formulating a MySQL query to do the following task, although I have seen similar queries discussed here, they are sufficiently different from this one to snooker my attempts to transpose them. 我在制定一个MySQL查询来执行以下任务时遇到了一个问题,尽管我在这里看到了类似的查询,但它们与这一查询有很大的不同,可以让我无意中调换它们。 The problem is (fairly) simple to state. 这个问题很(很)容易说明。 I have three tables, 'members', 'dog_shareoffered' and 'dog_sharewanted'. 我有三个表,“成员”,“ dog_shareoffered”和“ dog_sharewanted”。 Members may have zero, one or more adverts for things they want to sell or want to buy, and the details are stored in the corresponding offered or wanted table, together with the id of the member who placed the ad. 成员可能想要出售或购买的商品有零个,一个或多个广告,并且详细信息以及放置广告的成员的ID一起存储在相应的提供或想要的表中。 The column 'id' is unique to the member, and common to all three tables. 列“ id”对于成员而言是唯一的,并且对于所有三个表都是相同的。 The query I want is to ask how many members have NOT placed an ad in either table. 我要查询的是询问有多少个成员未在任一表中放置广告。

I have tried several ways of asking this. 我尝试了几种方法来问这个。 The closest I can get is a query that doesn't crash! 我能得到的最接近的查询不会崩溃! (I am not a MySQL expert by any means). (无论如何我都不是MySQL专家)。 The following I have put together from what I gleaned from other examples, but it returns zero rows, where I know the result should be greater than zero. 根据我从其他示例中收集的内容,以下内容进行了汇总,但返回零行,我知道结果应大于零。

SELECT id 
  FROM members 
 WHERE id IN (SELECT id 
                FROM dog_sharewanted 
               WHERE id IS NULL) 
   AND id IN (SELECT id 
                FROM dog_shareoffered 
               WHERE id IS NULL)

THis query looks pleasingly simple to understand, unlike the 'JOIN's' I've seen but I am guessing that maybe I need some sort of Join, but how would that look in this case? 与我所见过的“ JOIN”不同,该查询看起来很容易理解,但我猜测也许我需要某种Join,但是在这种情况下会怎样呢?

If you want no ads in either table, then the sort of query you are after is: 如果您在任何一个表中都不希望有广告,那么您要查询的是:

SELECT id
FROM members
WHERE id NOT IN ( any id from any other table )

To select ids from other tables: 要从其他表中选择ID,请执行以下操作:

SELECT id
FROM <othertable>

Hence: 因此:

SELECT id
FROM members
WHERE id NOT IN (SELECT id FROM dog_shareoffered)
 AND  id NOT IN (SELECT id FROM dog_sharewanted)

I added the 'SELECT DISTINCT' because one member may put in many ads, but there's only one id. 我添加了“ SELECT DISTINCT”,因为一个成员可能会投放许多广告,但是只有一个ID。 I used to have a SELECT DISTINCT in the subqueries above but as comments below mention, this is not necessary. 我以前在上面的子查询中有一个SELECT DISTINCT ,但是正如下面的注释所述,这不是必需的。

If you wanted to avoid a sub-query (a possible performance increase, depending..) you could use some LEFT JOINs: 如果要避免子查询(可能会提高性能,取决于..),则可以使用一些LEFT JOIN:

SELECT members.id
FROM members
LEFT JOIN dog_shareoffered
 ON dog_shareoffered.id = members.id
LEFT JOIN dog_sharewanted
 ON dog_sharewanted.id = members.id
WHERE dog_shareoffered.id IS NULL
  AND dog_sharewanted.id IS NULL

Why this works: 工作原理:

It takes the table members and joins it to the other two tables on the id column. 它接受表members并将其连接到id列上的其他两个表。 The LEFT JOIN means that if a member exists in the members table but not the table we're joining to (eg dog_shareoffered ), then the corresponding dog_shareoffered columns will have NULL in them. LEFT JOIN意味着,如果members表中存在一个成员,但我们要加入的表中存在该members (例如dog_shareoffered ),则对应的dog_shareoffered列中将为NULL

So, the WHERE condition picks out rows where there's a NULL id in both dog_shareoffered and dog_sharewanted , meaning we've found ids in members with no corresponding id in the other two tables. 因此, WHERE条件挑选出在dog_shareoffereddog_sharewanted中都具有NULL ID的行,这意味着我们在其他两个表中的members中没有对应的ID的情况下找到了ID。

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

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