且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

order by子句在mysql中如何工作,排序显示了奇怪的行为

更新时间:2023-12-03 17:41:22

此问题是因为您为订​​单指定了重复的值(又称先生"或太太"),因此无法保证选择的订单这是因为他们都是先生或太太.

The issue with this is because there is a duplicated value that you specify for your order by aka its either Mr. or Mrs. there is no guaranteed select order with this because they are all Mr. or Mrs.

如果您想确保它始终按特定顺序排列,则还应将主键作为第二个顺序包含在内,以使它们保持相同..aka

if you want to ensure that it is always going to be in a specific order then you should also include the primary key as a second ordering to keep it all the same.. aka

ORDER BY CON_PREFIX ASC, M_ID ASC

目前的状态ORDER BY CON_PREFIX确实为您提供了应该给您的东西,前缀按升序排列,与导致这种情况的限制没有任何关系,您只是没有告诉MySQL您还想要什么?数据返回给您.

as it stands right now ORDER BY CON_PREFIX is giving you exactly what it should be giving you, the prefixes in ascending order, there is nothing related to the limit for causing this, you simply just havent told MySQL how else you want the data to be returned to you.

回答您在问题中的PERSONAL OPINION编辑. 您所描述的内容是不合理的,请以这种方式考虑,因为指定了排序依据,因此用于提取数据的任何默认顺序现在都消失了.如果子句上没有排序依据,则mysql具有通用的选择模式,但是一旦在查询中放置排序依据,该模式就会消失

to respond to your PERSONAL OPINION edit in the question.... what you are describing is unreasonable, think of it this way any default order that would be used to pull out data is now gone because you are specifying an order by. if there wasn't any order by on the clause then mysql has a generic select pattern but again that is gone once you put an order by on the query