我有3個表:如何選擇MariaDB中每個組的最新成員?
- 賬戶 - 賬戶信息
- 機 - 機資訊
- account_machine - 帳戶映射到一臺機器上
每個帳戶由一個處理的日期機。隨着時間的推移,一個帳戶可以遷移到不同的機器上,但在某一天它只能由一臺機器來處理。如果帳戶不再有效,那麼相應的machine_id爲0。給定一個日期,我想找到所有活躍賬戶,所以我想出了這個查詢:
SELECT account.id
FROM account JOIN account_machine m
ON m.account_id=account.id && m.machine_id && m.machine_id=
(SELECT machine_id
FROM account_machine
WHERE account_id=account.id && date<=20170215
ORDER BY date DESC LIMIT 1)
GROUP BY account.id;
這正常工作與MySQL,但沒有按」跟MariaDB一起。
MariaDB [db]> select * from account_machine;
+------------+------------+------------+
| date | account_id | machine_id |
+------------+------------+------------+
| 2013-01-01 | 1 | 1 |
| 2013-01-01 | 8 | 1 |
| 2013-01-01 | 2 | 2 |
| 2013-01-01 | 3 | 2 |
| 2013-01-01 | 4 | 3 |
| 2013-01-01 | 12 | 3 |
| 2016-04-01 | 24 | 3 |
| 2013-01-01 | 5 | 5 |
| 2013-01-01 | 6 | 8 |
| 2013-01-01 | 7 | 6 |
| 2014-01-01 | 9 | 6 |
| 2013-01-01 | 10 | 4 |
| 2014-07-01 | 11 | 10 |
| 2014-01-01 | 13 | 7 |
| 2014-01-01 | 14 | 7 |
| 2014-07-01 | 15 | 11 |
| 2014-07-01 | 16 | 14 |
| 2014-07-01 | 17 | 12 |
| 2015-01-01 | 18 | 13 |
| 2015-01-01 | 19 | 13 |
| 2015-04-01 | 20 | 13 |
| 2015-04-01 | 21 | 7 |
| 2015-04-01 | 22 | 13 |
| 2016-04-01 | 23 | 15 |
| 2016-05-01 | 25 | 9 |
| 2016-05-19 | 26 | 4 |
| 2014-08-06 | 1 | 0 |
| 2016-01-15 | 12 | 0 |
| 2015-11-04 | 19 | 12 |
| 2016-05-23 | 10 | 0 |
| 2016-05-26 | 2 | 18 |
| 2016-05-27 | 13 | 16 |
| 2016-06-02 | 27 | 3 |
| 2016-06-02 | 4 | 0 |
| 2016-06-08 | 28 | 17 |
| 2016-06-21 | 29 | 19 |
| 2016-07-11 | 30 | 20 |
| 2016-08-15 | 13 | 0 |
| 2016-08-19 | 2 | 18 |
| 2016-08-25 | 31 | 21 |
| 2016-09-08 | 32 | 20 |
| 2016-11-30 | 19 | 12 |
| 2016-11-30 | 22 | 13 |
| 2017-01-20 | 33 | 15 |
+------------+------------+------------+
MariaDB [db]> select account.id from account join account_machine m on m.account_id=account.id && m.machine_id && m.machine_id=(select a.machine_id from account_machine a where a.account_id=account.id && a.date<=20170215 order by a.date desc limit 1) group by account.id;
+----+
| id |
+----+
| 23 |
| 33 |
+----+
mysql> select account.id from account join account_machine m on m.account_id=account.id && m.machine_id && m.machine_id=(select a.machine_id from account_machine a where a.account_id=account.id && a.date<=20170215 order by a.date desc limit 1) group by account.id;
+----+
| id |
+----+
| 2 |
| 3 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 11 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
| 31 |
| 32 |
| 33 |
+----+
P.S.這裏有3個表,爲您重現:
CREATE TABLE `account` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
INSERT INTO `account` VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31),(32),(33);
CREATE TABLE `account_machine` (
`date` date NOT NULL,
`account_id` smallint(5) unsigned NOT NULL,
`machine_id` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`date`,`account_id`)
) ENGINE=MyISAM;
INSERT INTO `account_machine` VALUES ('2013-01-01',1,1),('2013-01-01',8,1),('2013-01-01',2,2),('2013-01-01',3,2),('2013-01-01',4,3),('2013-01-01',12,3),('2016-04-01',24,3),('2013-01-01',5,5),('2013-01-01',6,8),('2013-01-01',7,6),('2014-01-01',9,6),('2013-01-01',10,4),('2014-07-01',11,10),('2014-01-01',13,7),('2014-01-01',14,7),('2014-07-01',15,11),('2014-07-01',16,14),('2014-07-01',17,12),('2015-01-01',18,13),('2015-01-01',19,13),('2015-04-01',20,13),('2015-04-01',21,7),('2015-04-01',22,13),('2016-04-01',23,15),('2016-05-01',25,9),('2016-05-19',26,4),('2014-08-06',1,0),('2016-01-15',12,0),('2015-11-04',19,12),('2016-05-23',10,0),('2016-05-26',2,18),('2016-05-27',13,16),('2016-06-02',27,3),('2016-06-02',4,0),('2016-06-08',28,17),('2016-06-21',29,19),('2016-07-11',30,20),('2016-08-15',13,0),('2016-08-19',2,18),('2016-08-25',31,21),('2016-09-08',32,20),('2016-11-30',19,12),('2016-11-30',22,13),('2017-01-20',33,15);
CREATE TABLE `machine` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
INSERT INTO `machine` VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22);
是表已在兩種模式中相同的數據? –
順便說一句'和m.machine_id'總是如此。如果你不屑於格式化您的疑問,這將是顯而易見的,你太 – Strawberry
見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-我將是一個非常簡單的sql查詢 – Strawberry