我尝试这个例子,但它不起作用。 而不是具有user_login名称,而是打印“当前”字符串。 我的感觉是:代码是静态执行的,因为我没有在wordpress中使用正确的插件来完成这项工作。
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(serial CHAR(3) NOT NULL
,date DATE NOT NULL
,value INT NOT NULL
,PRIMARY KEY (serial,date)
);
INSERT INTO my_table VALUES
('xxx','2016-01-03',12),
('xxx','2016-01-02',5),
('xxx','2016-01-01',3),
('yyy','2016-01-03',9),
('yyy','2016-01-02',6),
('yyy','2016-01-01',1);
SELECT x.*
, MAX(y.value) prev
FROM my_table x
LEFT
JOIN my_table y
ON y.serial = x.serial
AND y.date < x.date
GROUP
BY x.serial
, x.date;
+--------+------------+-------+------+
| serial | date | value | prev |
+--------+------------+-------+------+
| xxx | 2016-01-01 | 3 | NULL |
| xxx | 2016-01-02 | 5 | 3 |
| xxx | 2016-01-03 | 12 | 5 |
| yyy | 2016-01-01 | 1 | NULL |
| yyy | 2016-01-02 | 6 | 1 |
| yyy | 2016-01-03 | 9 | 6 |
+--------+------------+-------+------+