Select nearest date in the interval

时间:2019-04-17 00:04:12

标签: mysql sql

I'm trying to select rows in which 3+ posts is in the interval 14 days. For example:

    User    | id_post    | date    
    1       | 12         | 2018-01-01    
    1       | 13         | 2018-01-05    
    1       | 14         | 2018-01-21    
    1       | 15         | 2018-01-27    
    1       | 16         | 2018-01-29    
    2       | 17         | 2018-01-01    
    2       | 18         | 2018-01-20    
    2       | 19         | 2018-02-17    
    2       | 20         | 2018-03-07    
    2       | 21         | 2018-04-29

User = OwnerUserId

date = CreationDate

In this case I need to return just User 1 because he has posts which are in 14 days.

Please, help me how I can get it. Thank you

Update: A user should have posts which were published in the interval of 14 days. It can be more, for example if the last day is in 2019 but in 2018 there was 3posts published within 14 days - it's ok

now i have (data get from data.stackexchange stackoverflow) and tried to apply

select OwnerUserId from Posts  as p
where OwnerUserId in (select Users.id from Users WHERE YEAR (Users.CreationDate) >= 2017) 
AND YEAR (p.CreationDate) >= 2018 
AND p.Tags like '%sql%'
join (select OwnerUserId, CreationDate as startdate, dateadd(day,14,CreationDate) as enddate
from Posts) as r
 on p.OwnerUserId = r.OwnerUserId and p.CreationDate between r.startdate and r.enddate
 group by p.OwnerUserId, CreationDate
having count(*) >= 3

but it replies

Incorrect syntax near the keyword 'join'.
 Incorrect syntax near the keyword 'as'.

I'm a begginner here and in the sql, so i dont exactly know how to combine my previous 'filtr' and current join with date

4 个答案:

答案 0 :(得分:0)

I'll not tell you the solution, but give you some pseudo-code and you figure out how to code it in SQL-

a) You should restrict your data for just 14 days. b) Now, make groupings by User and find the count of records/lines present (for each User).

c) Now, again do a filter check to find users whose count of records is greater than 3.

Now, tell us which SQL keywords will be used for each points above.

答案 1 :(得分:0)

I think something like

select p.user_id
  from posts p
  join (select user_id, xdate start_date, date_add(xdate, interval 14 day) end_date
         from posts) r 
    on p.user_id = r.user_id and p.xdate between r.start_date and r.end_date
 group by user_id, start_date
having count(*) >= 3

can help. It may not be the best possible solution, but it works.

Check it on SQL Fiddle

答案 2 :(得分:0)

If you just want to select users by id you may try

Select id_post, date from yourtable where user = 2 order by id DESC limit 10;

You should have Colum called id with auto increment so new posts will have higher id so when it's sorted in descending it will start with post with higher id also you should have index on that id colum auto increment and index

If you don't want to use the above method then you will do that with date range like this

$date = gmdate() - (3600*24);  24 is 24 hours past
Select id_post, title from mutable where add_date > 'value of $date'

In both cases you should have index on user id The second query is what you need but you should get the date from the equation first then apply it to the query

答案 3 :(得分:0)

First, I think you mean user 1 not 2.

In MySQL 8+, this is pretty easy. If you want the first such post:

select t.*
from (select t.*,
             lead(date, 2) over (partition by user order by date) as next_date2
      from t
     ) t
where next_date2 <= date + interval 14 day;