简体   繁体   English

MySQL查询有了选择具有不同与限制条件

[英]MYSQL Query With Select Having Distinct with LIMIT Condition

Currently, I have a table as below and I wanted to pull the records with id having the estimated_date which are assigned to least value of update_index value and if the value is null I want to go the value assigned to next update_index, 目前,我有一个下表,我想提取ID为带有Estimate_date的记录,这些记录被分配给update_index值的最小值,如果该值为null,我想去分配给下一个update_index的值,

For example, for record with 例如,用于记录

  • id=73, I want to get the value 2017-06-13 00:00:00 id = 73,我想获取值2017-06-13 00:00:00
  • id=75, I want to get the value 2017-01-01 00:00:00 id = 75,我想获取值2017-01-01 00:00:00
  • id=76, I want to get the value 2018-06-01 00:00:00 id = 76,我想获取值2018-06-01 00:00:00

Input Table 输入表

+----+--------------+---------------------+
| 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 |
+----+--------------+---------------------+

Output Table 输出表

+----+--------------+---------------------+
| 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 |
+----+--------------+---------------------+

I have tried the below values but I get only one record always, can you please help me with this? 我尝试了以下值,但始终只能得到一个记录,您能帮我吗?

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);
 `

I would use a correlated subquery: 我将使用相关的子查询:

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
                       );

Please try this. 请尝试这个。

    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

You cant try this: 您不能尝试这样:

select t.id, t.update_index, t.estimated_date from tablename t where t.update_index=(select MIN(update_index) from tablename t1 where t1.id=t.id and t1.estimated_date is not null) group by t.id 从表名t选择t.id,t.update_index,t.estimated_date,其中t.update_index =(从表名t1选择MIN(update_index),其中t1.id = t.id并且t1.estimated_date不为空)按t.id分组

Use subquery with LIMIT clause : 使用带有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
                       );

However, your sample data more suggest me : 但是,您的样本数据进一步提示了我:

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
                         );

If you have a ties with estimated_date then first approach would work instead. 如果您与estimated_date有联系,则可以采用第一种方法。

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

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