简体   繁体   English

MySQL-选择已上传2个月以上的数据

[英]Mysql- Select data that has been uploaded more than 2 months

I have a table where I upload the users galleries. 我有一个上载用户图库的表。 I need the users with the gallery.created_at has been uploaded for 2 or more months. 我需要具有gallery.created_at的用户已上传了2个月或更长时间。 I have tried this but it is giving me 0 results 我已经尝试过了,但这给了我0个结果

    select `galleries`.`client_id` as `client_id`, 
        `users`.`first_name` as `first_name` from `galleries` 
        inner join `users` on `users`.`id` = `galleries`.`client_id` 
        where `galleries`.`session_id` is null and 
       `galleries`.`is_video` = '1' and `galleries`.`is_thumb` = '1' and
        galleries.created_at >= galleries.created_at - INTERVAL 2 MONTH 
   `galleries`.`client_id` not in (select `client_id` from 
        `orders`)

Problem is here: 问题在这里:

galleries.created_at >= galleries.created_at - INTERVAL 2 MONTH

and a missing AND . 和一个缺少的AND

Perhaps curdate is what you need: 也许您需要的是curdate

select g.client_id as client_id,
    u.first_name as first_name
from galleries g
inner join users u on u.id = g.client_id
where g.session_id is null
    and g.is_video = '1'
    and g.is_thumb = '1'
    and g.created_at <= curdate() - INTERVAL 2 MONTH
    and g.client_id not in (
        select client_id
        from orders
        )

I used <= because your requirements states uploaded for 2 or more months . 我使用<=是因为您的要求指出上传了2个月或更长时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM