选择与LIMIT条件不同的MYSQL查询

时间:2018-09-18 14:44:14

标签: mysql sql

当前,我有一个下表,我想提取ID为带有Estimate_date的记录,这些记录被分配给update_index值的最小值,如果该值为空,我想去分配给下一个update_index的值,< / p>

例如,用于

记录
  • id = 73,我想获取值2017-06-13 00:00:00
  • id = 75,我想获取值2017-01-01 00:00:00
  • id = 76,我想获取值2018-06-01 00:00:00

输入表

+----+--------------+---------------------+
| id | update_index |    estimated_date   |
+----+--------------+---------------------+
| 73 |       1      | 2017-06-13 00:00:00 |
+----+--------------+---------------------+
| 73 |       2      | 2017-01-13 00:00:00 |
+----+--------------+---------------------+
| 73 |       3      | 2017-05-13 00:00:00 |
+----+--------------+---------------------+
| 73 |       4      |         NULL        |
+----+--------------+---------------------+
| 75 |       1      | 2017-01-01 00:00:00 |
+----+--------------+---------------------+
| 75 |       2      |         NULL        |
+----+--------------+---------------------+
| 75 |       3      | 2019-01-01 00:00:00 |
+----+--------------+---------------------+
| 76 |       1      |         NULL        |
+----+--------------+---------------------+
| 76 |       2      | 2018-06-01 00:00:00 |
+----+--------------+---------------------+

输出表

+----+--------------+---------------------+
| id | update_index |    estimated_date   |
+----+--------------+---------------------+
| 73 |       1      | 2017-06-13 00:00:00 |
+----+--------------+---------------------+
| 75 |       1      | 2017-01-01 00:00:00 |
+----+--------------+---------------------+
| 76 |       2      | 2018-06-01 00:00:00 |
+----+--------------+---------------------+

我尝试了以下值,但我始终只能获得一条记录,您能帮我吗?

SELECT  id,update_index,estimated_date
FROM tablename where estimated_date = (
       SELECT DISTINCT estimated_date
       FROM tablename
        where estimated_date is not null
       ORDER BY id, udpate_index ASC
       LIMIT 1);
 `

4 个答案:

答案 0 :(得分:2)

我将使用相关的子查询:

select t.*
from tablename t
where t.update_index = (select t2.update_index
                        from tablename t2
                        where t2.id = t.id and t2.estimated_date is not null
                        order by t2.update_index asc
                        limit 1
                       );

答案 1 :(得分:1)

请尝试这个。

    select t2.*
    from
    (select id, min(update_index) updated_index 
    from input_table 
    where estimated_date is not null
    group by 1 )t1
    left join input_table t2
    on t1.id = t2.id
    and t1.update_index = t2.update_index

答案 2 :(得分:1)

您无法尝试:

选择t.id,t.update_index,t.estimated_date 来自表名t 其中t.update_index =(从表名t1中选择MIN(update_index),其中t1.id = t.id和t1.estimated_date不为null) 按t.id分组

答案 3 :(得分:1)

LIMIT子句中使用子查询:

select t.*
from table t
where t.update_index = (select t2.update_index
                        from table t2
                        where t2.id = t.id and t2.estimated_date is not null
                        order by t2.estimated_date desc
                        limit 1
                       );

但是,您的示例数据更能提示我:

select t.*
from table t
where t.estimated_date = (select max(t2.estimated_date)
                          from table t2
                          where t2.id = t.id and t2.estimated_date is not null
                         );

如果您与estimated_date有联系,则可以采用第一种方法。