Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL在满足其他条件后选择具有条件的最小行id_Mysql - Fatal编程技术网

MySQL在满足其他条件后选择具有条件的最小行id

MySQL在满足其他条件后选择具有条件的最小行id,mysql,Mysql,我有一个MySQL版本表。 我希望在最后一次更改后得到最小的发布版本published=1。 版本id越高,越新 以下是一些我希望返回的示例: +----+-----------+---------+ | id | published | changed | +----+-----------+---------+ | 4 | 1 | 0 | | 3 | 1 | 0 | | 2 | 1 | 0 | | 1

我有一个MySQL版本表。 我希望在最后一次更改后得到最小的发布版本published=1。 版本id越高,越新

以下是一些我希望返回的示例:

+----+-----------+---------+
| id | published | changed |
+----+-----------+---------+
|  4 |         1 |       0 |
|  3 |         1 |       0 |
|  2 |         1 |       0 |
|  1 |         1 |       1 |    <- I want this row
+----+-----------+---------+

+----+-----------+---------+
| id | published | changed |
+----+-----------+---------+
|  5 |         0 |       0 |
|  4 |         0 |       1 |
|  3 |         1 |       0 |
|  2 |         1 |       0 |    <- I want this row
|  1 |         0 |       1 |    
+----+-----------+---------+

+----+-----------+---------+
| id | published | changed |
+----+-----------+---------+
|  4 |         1 |       0 |
|  3 |         1 |       0 |
|  2 |         1 |       0 |
|  1 |         1 |       0 |     No rows should be returned
+----+-----------+---------+


+----+-----------+---------+
| id | published | changed |
+----+-----------+---------+
|  4 |         0 |       0 |
|  3 |         0 |       0 |
|  2 |         0 |       1 |
|  1 |         0 |       0 |      No rows should be returned
+----+-----------+---------+
在MySQL中,如果没有一些外部php逻辑,有什么更优雅的方法来处理这个问题

提前感谢,

利用,我们可以尝试确定至少一个id检查限制1的使用情况,该限制已更改为=1,并且小于或等于行id。 我们只考虑那些已经发表的行=1。 过滤掉那些先前使用EXISTS更改了ID的行。 使用ORDER BY ASC LIMIT 1,我们将最终结果集限制为最小id。 请尝试以下查询:

SELECT t1.id, 
       t1.published, 
       t1.changed 
FROM your_table AS t1 
WHERE t1.published = 1 
  AND EXISTS ( 
               SELECT t2.id 
               FROM your_table AS t2 
               WHERE t2.id <= t1.id 
                 AND t2.changed = 1 
               LIMIT 1
             )
ORDER BY t1.id ASC 
LIMIT 1

问题不清楚,您能更好地解释一下查询选择该行的方式和原因吗