My problem is this: I have two tables; people and teams, I want to select all the persons with the role_id = 2 that exist in persons, but not in teams.
The table teamsstores hashes for the team leader, who can simultaneously lead the team alone . When creating teams, I just want to show the administrators of people who do not currently lead a team, basically exclude all those who are already the leader of any 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` );
SQL Query , teams 1 , , MySQL , .
WHERE :
WHERE `persons`.`role_id` = 2 AND `persons`.`hash` != `teams`.`leader`
leader, teams
- LEFT JOIN, .
!
P.S.. SQL, , :
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');