MYSQL之前的最近一行,另一列具有特定值

时间:2014-03-12 10:38:50

标签: mysql

我一直试图将表连接到自身,具体取决于TIMESTAMP(此行最近)和另一列STABILIZATION(值为1)

查询测试表:

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `UID` varchar(40) NOT NULL,
  `CREATED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `STABILISATION` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`UID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

TRUNCATE `test`;
INSERT INTO `test` (`UID`, `CREATED`, `STABILISATION`) VALUES
('1',   '2014-03-12 09:22:41',  1),
('2',   '2014-03-12 09:24:20',  0),
('3',   '2014-03-12 09:24:35',  0),
('4',   '2014-03-12 09:24:39',  1),
('5',   '2014-03-12 09:24:41',  0),
('6',   '2014-03-12 10:02:17',  1),
('7',   '2014-03-12 10:02:24',  0),
('8',   '2014-03-12 11:00:15',  0),
('9',   '2014-03-12 11:01:08',  1),
('10',  '2014-03-12 11:01:17',  0);

我试图像这样获得t2_UID COLUMN

ID       CREATED                STABILISATION     t2_UID
'1',    '2014-03-12 09:22:41',  1,                1
'2',    '2014-03-12 09:24:20',  0,                1
'3',    '2014-03-12 09:24:35',  0,                1
'4',    '2014-03-12 09:24:39',  1,                4
'5',    '2014-03-12 09:24:41',  0,                4
'6',    '2014-03-12 10:02:17',  1,                6
'7',    '2014-03-12 10:02:24',  0,                6
'8',    '2014-03-12 11:00:15',  0,                6
'9',    '2014-03-12 11:01:08',  1,                9
'10',   '2014-03-12 11:01:17',  0,                9

每当STABILIZATION发生变化时,t2_UID也应该改变!

我最接近的是这个查询:

SELECT 
t1.*,
t2.UID AS t2_UID
FROM test AS t1
LEFT JOIN test as t2
ON t2.UID = (SELECT UID
FROM test as t3
WHERE t3.STABILISATION = 1
ORDER BY ABS(TIMEDIFF(t1.CREATED, t3.CREATED))
LIMIT 1)

最近的

2 个答案:

答案 0 :(得分:1)

这是一种快速而肮脏的方式:

SELECT *,
    (SELECT t2.UID 
    FROM test t2 
    WHERE STABILISATION=1 AND t2.CREATED <= t.CREATED 
    ORDER BY t2.CREATED DESC LIMIT 1) AS t2_id
FROM test t
ORDER BY t.CREATED;

SQL Fiddle

答案 1 :(得分:0)

为提供数据做得好......快速而干净的方式

 SELECT a.uid
      , a.created
      , a.stabilisation
      , b.uid
   FROM 
      ( SELECT x.*
             , MAX(y.created) max_created
          FROM test x 
          JOIN test y
            ON y.created <= x.created 
           AND y.stabilisation = 1
         GROUP 
            BY created
      ) a
   JOIN test b
     ON b.created = a.max_created;
+-----+---------------------+---------------+-----+
| uid | created             | stabilisation | uid |
+-----+---------------------+---------------+-----+
| 1a  | 2014-03-12 09:22:41 |             1 | 1a  |
| 0b  | 2014-03-12 09:24:20 |             0 | 1a  |
| 3c  | 2014-03-12 09:24:35 |             0 | 1a  |
| 0d  | 2014-03-12 09:24:39 |             1 | 0d  |
| 4e  | 2014-03-12 09:24:41 |             0 | 0d  |
| 0f  | 2014-03-12 10:02:17 |             1 | 0f  |
| 5g  | 2014-03-12 10:02:24 |             0 | 0f  |
| 3h  | 2014-03-12 11:00:15 |             0 | 0f  |
| 6i  | 2014-03-12 11:01:08 |             1 | 6i  |
| 7j  | 2014-03-12 11:01:17 |             0 | 6i  |
+-----+---------------------+---------------+-----+    

......或类似的东西