- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 3 张 table :
每个帐户由一台机器处理。随着时间的推移,一个帐户可以迁移到不同的机器,但在某一天它只能由一台机器处理。如果帐户不再有效,则相应的 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 |
+----+
附注这里有 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);
最佳答案
像这样的事情怎么办?
SELECT am1.account_id AS id
FROM account_machine am1
JOIN (
SELECT account_id, MAX(date) AS date
FROM account_machine
GROUP BY account_id
) am2
ON am1.account_id = am2.account_id
AND am1.date = am2.date
AND am1.machine_id != 0
ORDER BY am1.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 |
+----+
28 rows in set (0.00 sec)
我很想看看 MySQL 和 MariaDB 的 EXPLAIN EXTENDED/SHOW WARNINGS 的输出。这将准确地向您展示查询优化器如何重写查询。例如:
root@localhost [stack]> EXPLAIN EXTENDED SELECT am1.account_id AS id
-> FROM account_machine am1
-> JOIN (
-> SELECT account_id, MAX(date) AS date
-> FROM account_machine
-> GROUP BY account_id
-> ) am2
-> ON am1.account_id = am2.account_id
-> AND am1.date = am2.date
-> AND am1.machine_id != 0
-> ORDER BY am1.account_id\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 44
filtered: 100.00
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: am1
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 5
ref: am2.date,am2.account_id
rows: 1
filtered: 100.00
Extra: Using where
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: account_machine
type: index
possible_keys: NULL
key: PRIMARY
key_len: 5
ref: NULL
rows: 44
filtered: 100.00
Extra: Using index; Using temporary; Using filesort
3 rows in set, 1 warning (0.00 sec)
root@localhost [stack]> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: select `stack`.`am1`.`account_id` AS `id` from
`stack`.`account_machine` `am1` join (select
`stack`.`account_machine`.`account_id` AS
`account_id`,max(`stack`.`account_machine`.`date`) AS `date` from
`stack`.`account_machine` group by
`stack`.`account_machine`.`account_id`) `am2` where
((`stack`.`am1`.`account_id` = `am2`.`account_id`) and
(`stack`.`am1`.`date` = `am2`.`date`) and (`stack`.`am1`.`machine_id`
<> 0)) order by `stack`.`am1`.`account_id`
1 row in set (0.00 sec)
显然,没有索引就不是一个高性能的查询,但对于有限的数据集来说,它还可以。
关于mysql - 如何在MariaDB中选择每个组中的最新成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42292589/
是否有某种方法可以使用 JPA 或 Hibernate Crtiteria API 来表示这种 SQL?或者我应该将其作为 native 执行吗? SELECT A.X FROM (SELECT X,
在查询中, select id,name,feature,marks from (....) 我想删除其 id 在另一个 select 语句中存在的那些。 从 (...) 中选择 id 我是 sql
我想响应用户在 select 元素中选择一个项目。然而这个 jQuery: $('#platypusDropDown').select(function () { alert('You sel
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个回答) 关闭8年前。 我正在学习 SQL
This question already has answers here: “Notice: Undefined variable”, “Notice: Undefined index”, and
我在 php 脚本中调用 SQL。有时“DE”中没有值,如果是这种情况我想从“EN”中获取值 应该是这样的,但不是这样的 IF (EXISTS (SELECT epf_application_deta
这可能是一个奇怪的问题,但不知道如何研究它。执行以下查询时: SELECT Foo.col1, Foo.col2, Foo.col3 FROM Foo INNER JOIN Bar ON
如何在使用 Camera.DestinationType.FILE_URI. 时在 phonegap camera API 中同时选择或拾取多个图像我能够一次只选择一张图像。我可以使用 this 在
这是一个纯粹的学术问题。这两个陈述实际上是否相同? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 相对 IF EXIS
我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。 我的伪代码是这样的 Document divTag = Jsoup.connect(link
我正在处理一个具有多个选择框的表单。当用户从 selectbox1 中选择一个选项时,我需要 selectbox2 active 的另一个值。同样,当他选择 selectbox2 的另一个值时,我需要
Acme Inc. Christa Woods Charlotte Freeman Jeffrey Walton Ella Hubbard Se
我有一个login.html其中form定义如下: First Initial Plus Last Name : 我的do_authorize如下: "; pri
$.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data
我有一个下拉列表“磅”、“克”、“千克”和“盎司”。我想要这样一种情况,当我选择 gram 来执行一个函数时,当我在输入字段中输入一个值时,当我选择 pounds 时,我想要另一个函数来执行时我在输入
我有一个 GLSL 着色器,它从输入纹理的 channel 之一(例如 R)读取,然后写入输出纹理中的同一 channel 。该 channel 必须由用户选择。 我现在能想到的就是使用一个 int
我想根据下拉列表中的选定值生成输入文本框。 Options 2 3 4 5 就在这个选择框之后,一些输入字段应该按照选定的数字出现。 最佳答案 我建议您使用响应式(Reac
我是 SQL 新手,我想问一下如何根据首选项和分组选择条目。 +----------+----------+------+ | ENTRY_ID | ROUTE_ID | TYPE | +------
我有以下表结构: CREATE TABLE [dbo].[UTS_USERCLIENT_MAPPING_USER_LIST] ( [MAPPING_ID] [int] IDENTITY(1,1
我在移除不必要的床单时遇到了问题。我查看了不同的论坛并将不同的解决方案混合在一起。 此宏删除工作表(第一张工作表除外)。 Sub wrong() Dim sht As Object Applicati
我是一名优秀的程序员,十分优秀!