简体   繁体   English

MySQL-联接表中没有多少人?

[英]MySQL - How many people are NOT in a joined table?

I'm running the following query, which tells me which agents have ever been scheduled on a project at location 51: 我正在运行以下查询,该查询告诉我在项目51的位置上计划了哪些代理:

SELECT agents.agentid, agents.firstname, agents.lastname,
       schedule.projectid, projects.locationid
FROM agents
LEFT JOIN schedule USING (agentid)
LEFT JOIN projects USING (projectid)
WHERE (projects.locationid <=> 51)
GROUP BY agentid
ORDER BY agentid;

It yields 1249 rows (which is the correct result set). 它产生1249行(这是正确的结果集)。

If I then want to see the opposite, that is, agents that have NOT ever been scheduled on a project at location 51, I change the WHERE clause to: 如果然后我想看到相反的意思,即从未在项目的位置51调度过的代理,则将WHERE子句更改为:

WHERE NOT (projects.locationid <=> 51)

It yields 16169 rows. 它产生16169行。 1249+16169 = more rows than are in the agents table. 1249 + 16169 =行数超过代理表中的行数。

It's clearly because an agent can be involved in a project at more than one location, so when that happens, he shows up in both. 显然是因为一个代理可以在一个以上的位置参与一个项目,所以当这种情况发生时,他会出现在两个位置。

So my question is, how can I make this second query work? 所以我的问题是,如何使第二个查询有效? That is, how can I find out which agents have never been involved at a project at location 51? 也就是说, 我如何找出哪些代理从未参与过位置51的项目?

Thank you for any help! 感谢您的任何帮助!

Edit: Here are the table structures and sample data: 编辑:这是表结构和示例数据:

CREATE TABLE IF NOT EXISTS `agents` (
`agentid` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NOT NULL,
PRIMARY KEY (`agentid`));

CREATE TABLE IF NOT EXISTS `schedule` (
`scheduleid` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`agentid` SMALLINT(5) UNSIGNED NOT NULL,
`projectid` SMALLINT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`scheduleid`));

CREATE TABLE IF NOT EXISTS `projects` (
`projectid` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`locationid` SMALLINT(5) UNSIGNED NOT NULL,
PRIMARY KEY (`projectid`));

INSERT INTO `agents` (`agentid`,`firstname`, `lastname`)
VALUES (1, 'Bob', 'Smith'), (2, 'John','Doe'), (3, 'Jane','Doe'), (4, 'Sam','Foo'), (5, 'Emily','Bar');

INSERT INTO `projects` (`projectid`, `locationid`)
VALUES (1, 51), (2, 12), (3,15), (4,51), (5,99), (6,21), (7,51);

INSERT INTO `schedule` (`scheduleid`, `agentid`, `projectid`)
VALUES (1, 1, 1), (2, 2, 3), (3, 4, 3), (4, 1, 6), (5, 3, 5), (6, 5, 1), (7, 5, 3), (8, 5, 7), (9, 3, 6), (10, 4, 4);

Assuming that the LEFT OUTER joins are necessary (of which I'm not convinced), then you want the list of agents not in the list you obtained above. 假设有必要使用LEFT OUTER连接(我不认为其中的连接),那么您想要的代理列表不在上面获得的列表中。 Hence: 因此:

SELECT agents.agentid, agents.firstname, agents.lastname
  FROM agents
 WHERE agentid NOT IN
       (SELECT agents.agentid
          FROM agents
          LEFT JOIN schedule USING (agentid)
          LEFT JOIN projects USING (projectid)
         WHERE (projects.locationid = 51)
       )
 ORDER BY agentid;

Clearly, if you want to know which projects these agents have been involved in, then you reinstate the joins into the FROM clause of the outer query. 显然,如果您想知道这些代理所涉及的项目,则可以将连接恢复到外部查询的FROM子句中。

You could use the IN clause. 您可以使用IN子句。 Select all the agents that are not in the list of agents who have worked in location 51 : 选择不在位置51中工作过的业务代表列表中的所有业务代表:

SELECT
*
FROM agents
WHERE
agents.agentid NOT IN
 (
  SELECT agents.agentid
  FROM agents
  LEFT JOIN schedule USING (agentid)
  LEFT JOIN projects USING (projectid)
  WHERE (projects.locationid = 51)
  GROUP BY agentid
  ORDER BY agentid  
 )

do one thing first, add indexes in projects and schedule table. 首先要做一件事,在项目和进度表中添加索引。 This will improve performance a lot. 这将大大提高性能。 Then try this query 然后尝试此查询

SELECT
*
FROM agents
LEFT JOIN 
 (
  SELECT agents.agentid, schedule.scheduleid,schedule.projectid, projects.locationid
  FROM agents
  LEFT JOIN SCHEDULE USING (agentid)
  LEFT JOIN projects USING (projectid)
  WHERE (projects.locationid = 51)
  GROUP BY agentid
  ORDER BY agentid  
 ) AS t
 ON agents.agentid = t.agentid
WHERE t.agentid IS NULL

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

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