Return 在 MySql 中使用 max() 时出现意外设置
Return unexpected set while using max() in MySql
我需要在他们最后一次进入时设置 where STATUS = Need Corrections
。
对于给定的集合:
我需要第 3 行和第 5 行,因为订单 ORD001 的最后一个条目(即)max(entry) 是 2,订单 ORD003 的 max(entry) 是 1,它们的状态为 'Need Corrections'.
我尝试了这些查询但得到了错误的结果。
SELECT * 来自 QC WHERE STATUS = 'Need Corrections' AND ENTRY IN(SELECT MAX(ENTRY) FROM QC) GROUP BY PRD_ORDER;
SELECT * FROM QC WHERE STATUS = 'Need Corrections' AND ENTRY IN(SELECT MAX(ENTRY) FROM QC) ORDER BY PRD_ORDER;
你并不遥远
MariaDB [sandbox]> create table t(id int ,pr_order varchar(10),entry int, status varchar(20));
Query OK, 0 rows affected (0.14 sec)
MariaDB [sandbox]> insert into t values
-> (1,'o1',1,'NC'),
-> (2,'o2',1,'NC'),
-> (3,'o1',2,'NC'),
-> (4,'o2',2,'F'),
-> (5,'o3',1,'F');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select t.*
-> from t t
-> where t.status = 'NC'
-> and t.entry = (select max(t1.entry) from t t1 where t1.pr_order = t.pr_order);
+------+----------+-------+--------+
| id | pr_order | entry | status |
+------+----------+-------+--------+
| 3 | o1 | 2 | NC |
+------+----------+-------+--------+
1 row in set (0.00 sec)
我需要在他们最后一次进入时设置 where STATUS = Need Corrections
。
对于给定的集合:
我尝试了这些查询但得到了错误的结果。
SELECT * 来自 QC WHERE STATUS = 'Need Corrections' AND ENTRY IN(SELECT MAX(ENTRY) FROM QC) GROUP BY PRD_ORDER;
SELECT * FROM QC WHERE STATUS = 'Need Corrections' AND ENTRY IN(SELECT MAX(ENTRY) FROM QC) ORDER BY PRD_ORDER;
你并不遥远
MariaDB [sandbox]> create table t(id int ,pr_order varchar(10),entry int, status varchar(20));
Query OK, 0 rows affected (0.14 sec)
MariaDB [sandbox]> insert into t values
-> (1,'o1',1,'NC'),
-> (2,'o2',1,'NC'),
-> (3,'o1',2,'NC'),
-> (4,'o2',2,'F'),
-> (5,'o3',1,'F');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select t.*
-> from t t
-> where t.status = 'NC'
-> and t.entry = (select max(t1.entry) from t t1 where t1.pr_order = t.pr_order);
+------+----------+-------+--------+
| id | pr_order | entry | status |
+------+----------+-------+--------+
| 3 | o1 | 2 | NC |
+------+----------+-------+--------+
1 row in set (0.00 sec)