我创建了两张桌子和一张桌子.插入的值如下所示.
表格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');
表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');
当我执行查询
SELECT myId,@rowid:=@rowid+1 as myrow
FROM maxID,(SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;
我输出为
+++++++++++++
myid + myrow
+++++++++++++
A003 + 4
+++++++++++++
和
当我执行查询
SELECT myId,@rowid:=@rowid+1 as myrow
FROM maxID2,(SELECT @rowid:=0) as init
ORDER BY myrow desc
LIMIT 1;
我输出为
+++++++++++++
myid + myrow
+++++++++++++
A004 + 4
+++++++++++++
两个表之间的区别在于,在第二个表中,我将myID作为PRIMARY KEY.
您可以在www.sqlfiddle.com查看上述数据/结果.
我的问题是
为什么在查询相同时我得到两个不同的结果?
注意:这个问题与我的旧问题Getting last record from mysql有点相关,我几乎得到了答案,Yak通知我,行的顺序无法保证.