I have created two tables & inserted values as shown below .
Table 1
create table maxID (myID varchar(4));
insert into maxID values ('A001');
insert into maxID values ('A002');
insert into maxID values ('A004');
insert into maxID values ('A003');
Table 2
create table maxID2 (myID varchar(4) PRIMARY KEY);
insert into maxID2 values ('A001');
insert into maxID2 values ('A002');
insert into maxID2 values ('A004');
insert into maxID2 values ('A003');
When I execute query
SELECT myId, @rowid:=@rowid+1 as myrow
FROM maxID, (SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;
I get output as
+++++++++++++
myid + myrow
+++++++++++++
A003 + 4
+++++++++++++
AND
When I execute query
SELECT myId, @rowid:=@rowid+1 as myrow
FROM maxID2, (SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;
I get output as
+++++++++++++
myid + myrow
+++++++++++++
A004 + 4
+++++++++++++
The difference between two table is that in second table I have myID as PRIMARY KEY
.
You can view above data/ result at www.sqlfiddle.com.
My Question is
Why I am getting two different results when query is same?
NOTE : This question is bit related to my old question Getting last record from mysql, where I almost got the answer and Yak informed me that the order of rows are not guaranteed. :(