MySQL:查询(子查询)中的查询不起作用

时间:2019-06-13 09:48:37

标签: mysql subquery

我想在一个表上运行一个MySQL查询,然后在这个表上运行一个子查询。我有一个对象列表。每个对象都有一个主要版本和一个次要版本。对于一个对象,我试图找到哪个是该对象的“最后一个版本”:这意味着我想找到该对象的max(主要),然后在最后一个结果中找到max(minor)。

我构建了一个“测试” MySQL数据库:

create database test ;  
use test ;

我创建了一个表:

create table test 
(  
    id int auto_increment,   
    object varchar(10),   
    major int,   
    minor int,  
    PRIMARY KEY (`id`)  
) engine innodb;  

我用数据填充该表:

insert into test (object, major, minor) values ('obj1',1,0) ;  
insert into test (object, major, minor) values ('obj1',1,1) ;  
insert into test (object, major, minor) values ('obj1',1,2) ;  
insert into test (object, major, minor) values ('obj1',2,0) ;  
insert into test (object, major, minor) values ('obj1',2,1) ;  

我列出了表格:     从测试中选择*;

+----+--------+-------+-------+  
| id | object | major | minor |  
+----+--------+-------+-------+  
|  1 | obj1   |     1 |     0 |  
|  2 | obj1   |     1 |     1 |  
|  3 | obj1   |     1 |     2 |  
|  4 | obj1   |     2 |     0 |  
|  5 | obj1   |     2 |     1 |  
+----+--------+-------+-------+  
  

组5行(0.01秒)

第一个查询是获取max(major)行:

select * 
from test 
where object = 'obj1' 
  and major = (select max(major) from test);

结果如下:

+----+--------+-------+-------+  
| id | object | major | minor |  
+----+--------+-------+-------+  
|  4 | obj1   |     2 |     0 |  
|  5 | obj1   |     2 |     1 |  
+----+--------+-------+-------+  
  

组2行(0.00秒)

然后我尝试获取次要0版本:

select * 
from 
    (select * 
     from test 
     where object = 'obj1' 
       and major = (select max(major) from test)) as t 
where 
    t.minor = 0 ;

它有效,结果是:

+----+--------+-------+-------+  
| id | object | major | minor |  
+----+--------+-------+-------+  
|  4 | obj1   |     2 |     0 |  
+----+--------+-------+-------+  

但是我想要最后一个版本,所以我想找到max(minor)为1:

select * 
from 
    (select * 
     from test 
     where object = 'obj1' 
       and major = (select max(major) from test)) as t 
where 
    t.minor = (select max(minor) from t) ;

我得到了错误:

  

错误1146(42S02):表'test.t'不存在

我不明白为什么它不起作用。

谢谢

3 个答案:

答案 0 :(得分:2)

select * from test where object = 'obj1' order by major desc, minor desc limit 1;

首先,我们对表格进行排序,以使所有具有main列的最大值的行都排在最前面(按major desc排序)。如果有任何行的主列值具有相同的值,则我们通过对行进行排序以使它们具有与主列相同的值,从而对它们“排序”,从而使具有次要列的最大值的行排在第一位( order by次要说明)。因此,第一行的输出将是具有major最大值的行,而对于 major 值而言,其值为minor的最大值。由于我们只对第一行感兴趣,因此将输出限制为一行(限制1 )。

答案 1 :(得分:0)

您可以通过以下查询实现相同的目的

select * from test where object='obj1' and major=(select max(major) from test)
and minor = (
select max(minor) from test where object='obj1' and major=(select max(major) from test )
  )

这将给出以下结果

+----+--------+-------+-------+
| id | object | major | minor |
+----+--------+-------+-------+
| 5 | obj1 | 2 | 1 |
+----+--------+-------+-------+

答案 2 :(得分:0)

替换

select * from (select * from test where object='obj1' and major=(select max(major) from test) ) as t where t.minor=(select max(minor) from t)

select * from (select * from test where object='obj1' and major=(select max(major) from test) ) as t where t.minor=(select max(minor) from test)

因为您在上一个子查询中提到了t而不是测试