gpt4 book ai didi

MYSQL:选择更改的行,其中

转载 作者:行者123 更新时间:2023-11-30 01:34:32 25 4
gpt4 key购买 nike

1 -

create table changes(snapdate date,value int, uid int); 
insert into changes values
('2013-04-22', 0, 1 ),
('2013-04-21', 1,1 ),
('2013-04-20', 1,1 ),
('2013-04-19', 1,1 ),
('2013-04-19', 0,2 ),
('2013-04-19', 1,1 ),
('2013-04-18', 0,1 ),
('2013-04-17', 0,1 ),
('2013-04-17', 1,2 ),
('2013-04-16', 1,1 ),
('2013-04-16', 0 ,2);

2 -

SELECT a.snapdate, a.value 
FROM (
SELECT t1.*, COUNT(*) AS rank
FROM changes t1
LEFT JOIN changes t2 ON t1.snapdate >= t2.snapdate
GROUP BY t1.snapdate
) AS a
LEFT JOIN (
SELECT t1.*, COUNT(*) AS rank
FROM changes t1
LEFT JOIN changes t2 ON t1.snapdate >= t2.snapdate
GROUP BY t1.snapdate
) AS b ON a.rank = b.rank+1 AND a.value = b.value
WHERE b.snapdate IS NULL
ORDER BY a.snapdate DESC;

工作正常,但如何选择 WHERE uid=2 ?我无法使用临时表:

 create temporary table changes_temp
as
select *
from changes
where uid = 2

http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html

最佳答案

尝试在子查询中添加 WHERE 子句

    SELECT a.snapdate, a.value 
FROM (
SELECT t1.*, COUNT(*) AS rank
FROM changes t1
LEFT JOIN changes t2 ON t1.snapdate >= t2.snapdate
WHERE t2.uid=t1.uid AND t2.uid=2
GROUP BY t1.snapdate
) AS a
LEFT JOIN (
SELECT t1.*, COUNT(*) AS rank
FROM changes t1
LEFT JOIN changes t2 ON t1.snapdate >= t2.snapdate
WHERE t2.uid=t1.uid AND t2.uid=2
GROUP BY t1.snapdate
) AS b ON a.rank = b.rank+1 AND a.value = b.value
WHERE b.snapdate IS NULL
ORDER BY a.snapdate DESC;

已创建 SQLFiddle供测试用。那是你要的吗 ?您没有解释查询的目标

关于MYSQL:选择更改的行,其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com