问题
I have a simple query
SELECT * FROM table_name order by non_unique_column LIMIT 0,50
This gives me list of records in some order.
But when I am removing *
and putting some column names
from the table, then it is changing the list order.
For some
combination of columns in select clause it is giving different order.
I am not able to find, what pattern of column names in select clause gives different ordering.
So I am asking does any one had this kind of problem? or can any one suggest how the order by clause works in this scenario?
Note: If I am adding all the columns in select clause it gives me same result as *.
Please feel free to suggest any possible suggestion.
Thanks.
Update: Also when I am changing limit clause like LIMIT 0,50
to LIMIT 0,15
, I am getting different sets of records.
Update2:
I have created a sqlfiddle :
http://sqlfiddle.com/#!2/8f2340/3
Please see the result from below queries
SELECT `M_ID`, `CON_FRST_NM`, `CON_PREFIX`, `CON_LST_NM`, `CON_MOB`,`CON_FAX` FROM `sample_table` order by `CON_PREFIX` LIMIT 0,60
SELECT `M_ID`, `CON_FRST_NM`, `CON_PREFIX`, `CON_LST_NM`, `CON_MOB` FROM `sample_table` order by `CON_PREFIX` LIMIT 0,60
The only difference in the query is CON_FAX
is not present in select clause of second query.
Please help me out why this difference is coming.
And also please see the result of below queries with different limit clause. Note: It is second query from above queries.
SELECT `M_ID`, `CON_FRST_NM`, `CON_PREFIX`, `CON_LST_NM`, `CON_MOB` FROM `sample_table` order by `CON_PREFIX` LIMIT 0,60
SELECT `M_ID`, `CON_FRST_NM`, `CON_PREFIX`, `CON_LST_NM`, `CON_MOB` FROM `sample_table` order by `CON_PREFIX` LIMIT 0,15
Thanks.
PERSONAL OPINION: I see this as a bug/defect, because even if I haven't defined any unique order by clause and mysql is applying some random ordering based on its calculations, I feel it should not depend on what is present in select clause. My problem is for a given set of records it should return same ordered records regardless of whatever column I am selecting.
回答1:
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.
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
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.
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
回答2:
The order by clause works from left to right
Let me give an example
If you use SELECT serialNo,Name FROM table_name order by serialNo,Name;
what this does it it will order serialNo in the first instance and then it will order Name in the second instance . When it orders name in the second instance the oredering of the serialNo will change according to the ordering of the name .
Hope I was clear in my explanation .
来源:https://stackoverflow.com/questions/28061227/how-order-by-clause-works-in-mysql-ordering-shows-wierd-behaviour