Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MySQL, What is the best way to not to select users that exist in a different table?
    primarykey
    data
    text
    <p>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 <code>persons</code> but <strong>not</strong> in teams.</p> <p>Table <code>teams</code> stores the hashes for the <em>team leader</em> who can only lead <strong>one</strong> 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.</p> <p>My structure is as follows:</p> <pre><code>mysql&gt; 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&gt; 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) </code></pre> <p>My current SQL is as follows:</p> <pre><code>SELECT CONCAT( `persons`.`firstname` ," ", `persons`.`lastname` ) AS `manager`, `hash` FROM `persons` WHERE `persons`.`role_id` =2 AND `persons`.`hash` != (SELECT `leader` FROM `teams` ); </code></pre> <p>The latter SQL Query works when the table <code>teams</code> only has 1 record, but as soon as I add another one, MySQL complaints about the subquery producing two records.</p> <p>In the <strong>WHERE</strong> Clause, instead of subqueries I've also tried the following:</p> <pre><code> WHERE `persons`.`role_id` = 2 AND `persons`.`hash` != `teams`.`leader` </code></pre> <p>but then it complaints about column <code>leader</code> not existing in table <code>teams</code></p> <p>I was also thinking about using some kind of <em>inverse</em> LEFT JOIN, but I haven't been able to come up with an optimal solution.</p> <p>Any help is greatly appreciated! Thanks</p> <p><strong>P.S.</strong>: Here is the SQL statements should you want to have a scenario similar to mine:</p> <pre><code>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'); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload