[英]MySQL, selecting rows based on date column
I have a table with two columns, like: 我有一个包含两列的表格,例如:
X Date
A 2017-08-10
B 2016-12-19
C 2017-06-13
D 2017-07-20
E 2017-07-14
I would like to avoid selecting data which is more than one week older than the data in a row with X=D. 我想避免选择比X = D连续的数据早一个星期以上的数据。 I tried some queries:
我尝试了一些查询:
SELECT * FROM table WHERE Date < (SELECT Date FROM table where X='D') - INTERVAL 1 WEEK
but it isn't work for me. 但这对我不起作用。 The output should be like:
输出应类似于:
X Date
A 2017-08-10
D 2017-07-20
E 2017-07-14
I'm close to solve this problem I think but a little help can be significant for me. 我认为我已经可以解决这个问题,但是对我来说一点帮助可能是重要的。
Here's a fish 这是一条鱼
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.date - INTERVAL 1 WEEK < x.date
AND y.x = 'd';
SELECT * FROM table WHERE
Date > (SELECT DATE_SUB(Date, INTERVAL 7 DAY) FROM table where X='D')
or 要么
SELECT * FROM table
WHERE Date > (SELECT DATE_SUB(Date, INTERVAL 1 WEEK) FROM table where X='D')
You can check here SQL Fiddle Demo 您可以在此处查看SQL Fiddle演示
Try above query. 请尝试上述查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.