I want to run a MySQL query on a table then a subquery on this table. I have a list of objects. Each object has a major and a minor version. For one object I'm trying to find which is the "LAST VERSION" of this object : this mean that I want to find the max(major) for this object and then the max(minor) on the last result.
I build a "test" MySQL database :
create database test ;
use test ;
I created a table :
create table test
(
id int auto_increment,
object varchar(10),
major int,
minor int,
PRIMARY KEY (`id`)
) engine innodb;
I fill this table with data :
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) ;
I list the table : select * from test;
+----+--------+-------+-------+
| 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 rows in set (0,01 sec)
The first query is to get the max(major) lines :
select *
from test
where object = 'obj1'
and major = (select max(major) from test);
with this result :
+----+--------+-------+-------+
| id | object | major | minor |
+----+--------+-------+-------+
| 4 | obj1 | 2 | 0 |
| 5 | obj1 | 2 | 1 |
+----+--------+-------+-------+
2 rows in set (0,00 sec)
Then I try to get the minor 0 version :
select *
from
(select *
from test
where object = 'obj1'
and major = (select max(major) from test)) as t
where
t.minor = 0 ;
and it works, the result is :
+----+--------+-------+-------+
| id | object | major | minor |
+----+--------+-------+-------+
| 4 | obj1 | 2 | 0 |
+----+--------+-------+-------+
but I want the LAST VERSION, so I want to find the max(minor) which is 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) ;
And I get the error :
ERROR 1146 (42S02): Table 'test.t' doesn't exist
I don't understand why it doesn't work.
Thanks