简体   繁体   English

如何编写使用同一表中列的 MIN() 值的 WHERE 子句?

[英]How to write a WHERE clause that uses the MIN() value of a column in the same the table?

I need to write a query with the following logic:我需要使用以下逻辑编写查询:

Get all rows that have state = "active", that have retries < max_retries , and (from those qualifying rows) only return the rows with the least retries .获取所有具有state = "active" 的行,具有retries < max_retries ,并且(从那些符合条件的行中)只返回retries最少的行。

I have this query:我有这个查询:

SELECT * 
FROM users 
WHERE state = 'active' 
AND   retries < max_retries 
AND   retries = (SELECT MIN(retries) FROM users);

my table:我的表:

+-----------------------------------------+  
| id   uid   retries  max_retries  state  |  
+-----------------------------------------+  
| 1   Jack     3          5        active |  
| 2   Jill     1          3        active |  
| 3   John     3          3        active |  
| 4   James    0          5        no     |  
| 5   Jim      2          7        no     |  
+-----------------------------------------+  

In this case, my query should return the row containing Jill because Jill has: state = active, retries < max_retries and the smallest number of retries.在这种情况下,我的查询应该返回包含Jill的行,因为 Jill 具有:state = active、retries < max_retries 和最小重试次数。

However, my attempts always get some incorrect results.但是,我的尝试总是得到一些不正确的结果。 I looked at self join and union etc, but I can't seem to figure out how to solve it.我看着 self join 和 union 等,但我似乎无法弄清楚如何解决它。

[EDIT]: I think I figured it out now, but could someone please confirm it? [编辑]:我想我现在想通了,但有人可以确认一下吗?

SELECT * 
FROM users
WHERE state = 'active'
AND  retries =(SELECT MIN(retries) FROM users WHERE retries < max_retries )

After adding the state and max_enteies conditions to the main query and the subquery, I am now achieving the expected results.在主查询和子查询中添加statemax_enteies条件后,我现在达到了预期的结果。

SELECT 
    *
FROM
    users
WHERE
    state = 'active'
    AND retries < max_retries
    AND retries = (
        SELECT 
            MIN(retries)
        FROM
            users
        WHERE
            state = 'active'
            AND retries < max_retries
    )

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

相关问题 在我两次使用相同的表联接之后,如何在WHERE子句中调用具有相同列名的值? - After I Join with same table twice then how to call value with same column name in WHERE clause? 从使用WHERE子句的MySQL数据库表中的列中全选 - Selecting all from column in MySQL database table that uses WHERE clause 等同于where子句中同一表中的同一列 - equating same column from the same table in where clause 如何将默认值写入列,其中默认值是从使用另一列的函数创建的值? - How do I write a default value to a Column where the default value is a value created from a function that uses another Column? 如何使用 WHERE 子句更新表列? - How to update table column using WHERE clause? 如何将两个值的where子句放在同一字段中? - How to where clause of two value in the same field? 如何编写查询以基于多值字段从表过滤器(WHERE子句)获取值? - How to write a query to get values from a table filter(WHERE clause) based on multiple value field? 如何显示具有相同最小值的 SUM(column) 的所有 MIN? - How to show all MIN of SUM(column) that has the same min value? 更新 MySQL 表行 where 和 set 子句列的相同值 - Update MySQL table rows where and set clause same values of the column 如何使用 select 和同一张表上的 where 子句更新 sql 中的某些列? - How to update certain column in sql using select and where clause on the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM