Why does the query give two different results?

I created two tables and inserted the 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 the request

SELECT myId, @rowid:=@rowid+1 as myrow 
FROM maxID, (SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;

I get the output as

+++++++++++++
myid + myrow
+++++++++++++
A003 + 4
+++++++++++++

and

When I execute the request

SELECT myId, @rowid:=@rowid+1 as myrow 
FROM maxID2, (SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;

I get the output as

+++++++++++++
myid + myrow
+++++++++++++
A004 + 4
+++++++++++++

The difference between the two tables is that in the second table I have myID as PRIMARY KEY.

You can view the above data / result at www.sqlfiddle.com .

My question

Why do I get two different results when the request is the same?

NOTE: This question is slightly related to my old question, Getting the last record from mysql , where I almost got the answer, and Yak informed me that the order of the rows is not guaranteed .: (

+5
source share
2 answers

, , , fullscan.

, , , , , .

4- , , mysql (oracle, sql-...) , .

, , , , , .

, , mysql (, ), , , DESC, , mysql :

index_col_name ASC DESC. . , ; .

+7

Primary keys are returned in sorted order by default

.

:

+4

All Articles