简体   繁体   中英

Using MySQL, What is the best way to not to select users that exist in a different table?

My problem is the following: I have two tables; persons and teams, I want to select all the persons with role_id = 2, that exist in persons but not in teams.

Table teams stores the hashes for the team leader who can only lead one team at a time. When creating teams, I just want to show administrators the people who is not currently leading a team, basically exclude all the ones who are already leaders of any given team.

My structure is as follows:

mysql> desc persons;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| firstname   | varchar(9)  | YES  |     | NULL    |       |
| lastname    | varchar(10) | YES  |     | NULL    |       |
| role_id     | int(2)      | YES  |     | NULL    |       |
| hash        | varchar(32) | NO   | UNI | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

mysql> desc teams;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(20) | YES  |     | NULL    |                |
| leader | varchar(32) | NO   |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

My current SQL is as follows:

SELECT CONCAT(  `persons`.`firstname` ," ",  `persons`.`lastname` ) AS  `manager`,
`hash` FROM  `persons`
WHERE  `persons`.`role_id` =2 AND  `persons`.`hash` != 
(SELECT  `leader` FROM  `teams` );

The latter SQL Query works when the table teams only has 1 record, but as soon as I add another one, MySQL complaints about the subquery producing two records.

In the WHERE Clause, instead of subqueries I've also tried the following:

 WHERE `persons`.`role_id` = 2 AND `persons`.`hash` != `teams`.`leader`

but then it complaints about column leader not existing in table teams

I was also thinking about using some kind of inverse LEFT JOIN, but I haven't been able to come up with an optimal solution.

Any help is greatly appreciated! Thanks

PS : Here is the SQL statements should you want to have a scenario similar to mine:

DROP TABLE IF EXISTS `teams`;
CREATE TABLE IF NOT EXISTS `teams` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(20) DEFAULT NULL,
   `leader` varchar(32) NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

 INSERT INTO `teams` (`id`, `name`, `leader`) VALUES
 (1, 'Team 1', '406a3f5892e0fcb22bfc81ae023ce252'),
 (2, 'Team 2', 'd0ca479152996c8cabd89151fe844e63');


DROP TABLE IF EXISTS `persons`;
CREATE TABLE IF NOT EXISTS `persons` (
  `firstname` varchar(9) DEFAULT NULL,
  `lastname` varchar(10) DEFAULT NULL,
  `role_id` int(2) DEFAULT NULL,
  `hash` varchar(32) NOT NULL,
  PRIMARY KEY `hash` (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `persons` (`firstname`, `lastname`, `role_id`,`hash`) VALUES
('John', 'Doe', 2, '406a3f5892e0fcb22bfc81ae023ce252'),
('Jane', 'Doe', 2, 'd0ca479152996c8cabd89151fe844e63'),
('List', 'Me', 2, 'fbde2c4eeee7f455b655fe4805cfe66a'),
('List', 'Me Too', 2, '6dee2c4efae7f452b655abb805cfe66a');

You don't need a subquery to do that. A LEFT JOIN is enough:

SELECT
   CONCAT (p.firstname, " ", p.lastname) AS manager
   , p.hash
FROM  persons p
   LEFT JOIN teams t ON p.hash = t.leader
WHERE
   p.role_id = 2
   AND t.id IS NULL -- the trick

I think you want an IN clause.

SELECT CONCAT(  `persons`.`firstname` ," ",  `persons`.`lastname` ) AS  `manager`,
`hash` FROM  `persons`
WHERE  `persons`.`role_id` =2 AND  `persons`.`hash` NOT IN 
(SELECT  `leader` FROM  `teams` );

As pointed out, this is not optimal. You may want to do a join instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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