· 7 years ago · Nov 01, 2018, 05:26 AM
1/* create table */
2CREATE TABLE IF NOT EXISTS `users` (
3 `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
4 `email` varchar(255) NOT NULL,
5 `score` int(6) NOT NULL,
6 `company_id` int(6) NOT NULL,
7 PRIMARY KEY (`id`)
8) ENGINE=MyISAM DEFAULT CHARSET=utf8;
9INSERT INTO `users` (`email`, `score`, `company_id`) VALUES
10 ('user1@test.home', '8', '1'),
11 ('user2@test.home', '8', '2'),
12 ('user3@test.home', '4', '1'),
13 ('user4@test.home', '1', '2'),
14 ('user5@test.home', '2', '1'),
15 ('user6@test.home', '3', '2'),
16 ('user7@test.home', '0', '1'),
17 ('user8@test.home', '6', '2'),
18 ('user9@test.home', '9', '1')
19
20/* parse table */
21SELECT a.*
22FROM users a
23LEFT OUTER JOIN users b
24 ON a.company_id = b.company_id AND a.score < b.score
25WHERE b.company_id IS NULL;