简体   繁体   English

'NOT GREATER THAN'条件Mysql

[英]'NOT GREATER THAN' condition Mysql

I have a mysql table with columns userid and createdon where createdon column has DATE datatype. 我有一个mysql表,其中包含useridcreatedon ,其中createdon列具有DATE数据类型。

---------------------------------------------------------------------------------
ID                      UserId                         CreatedOn
1                         65                           2013-06-13 
2                         54                           2013-07-03
3                         34                           2013-08-23
4                         65                           2013-09-13 
5                         89                           2013-09-13

Now I want the userids where last createdon was before 2013-09-08. 现在我想要最后创建的用户标识在2013-09-08之前。 The correct answer will be userids 54,34 正确的答案是用户ID 54,34

Using 运用

select userid from table  where createdon <'2013-09-08'

returns 65,54,34 and usind 返回65,54,34和usind

select userid from table where userid notin (select userid from table where createdon > '2013-09-07')

takes a lot of time. 花了很多时间。

How to get the rows where last createdon < 2013-09-08 如何获取最后创建的行<2013-09-08

Try 尝试

SELECT UserID, MAX(CreatedOn) CreatedOn
  FROM table1
 GROUP BY UserId
HAVING CreatedOn < '2013-09-08'

Output: 输出:

| USERID |                     CREATEDON |
|--------|-------------------------------|
|     34 | August, 23 2013 00:00:00+0000 |
|     54 |   July, 03 2013 00:00:00+0000 |

Here is SQLFiddle demo 这是SQLFiddle演示

Your expectation is wrong. 你的期望是错误的。 The first row of the table you posted would meet your criteria ( '2013-06-'13' < '2013-09-08' ). 您发布的表格的第一行符合您的条件( '2013-06-'13' < '2013-09-08' )。 So a resultset with 65,54,34 is as expected. 因此 65,54,34的结果集符合预期。

Misunderstood the question. 误解了这个问题。

What you want is: 你想要的是:

    SELECT DISTINCT
       UserId
    FROM
       tbl outerTbl
    WHERE NOT EXISTS (
         SELECT *
         FROM tbl innerTbl
         WHERE
             outerTbl.UserId = innerTbl.UserId
             AND innerTbl.createdOn >= '2013-09-08'
    )

Try using STR_TO_DATE() function 尝试使用STR_TO_DATE()函数

SELECT userid FROM table  
WHERE createdon <= STR_TO_DATE('2013-09-08', '%Y-%m-%d')

Your query is correct, but probably you are not using proper data type for createdon column. 您的查询是正确的,但可能您没有为createdon列使用正确的数据类型。 Check that it works as expected in this SQLFiddle : 检查它在此SQLFiddle中是否按预期工作:

SELECT userid
FROM mytable
WHERE createdon < '2013-09-08'

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

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