Solved

Turning off the automatic Ascending order in MySQL for results

Posted on 2016-07-27
4
28 Views
Last Modified: 2016-08-01
MySQL is automatically ordering my result set in alphabetical or ascending order by the first column noted in the Select clause. How do I turn this off so that the results will show in the order in which the records appear in the database?
0
Comment
Question by:chonabraham
  • 2
4 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 41731750
There is no guarantee on the order of records if you do not specify ORDER BY explicitly. so include ORDER BY and mention whatever the column you want.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41731777
SQL databases including MySQL usually use a tree structure internally to store records.  It is not linear like a spreadsheet.  The tree structure makes a lot of things easier in a database.  

When a record in the tree is deleted, the next record that is inserted takes it's place.  Because of that and the tree structure itself, there isn't any 'default' sorting in an SQL table.  Like Sharath said above, use ORDER BY if you want a particular order to the results.
0
 

Author Comment

by:chonabraham
ID: 41732171
But in Access, the results are displayed the way they are entered into the table so then you must use order by to get results in asc or desc by a particular column or collection of columns. I converted an Access table into script then ran it in MySQL to create and insert data into a table in the same order as it appears in the original Access table but when I select from the MySQL table the result is automatically sorted in alphabetical order by the first attribute in the select clause. Therefore, I thought there must be some kind of default setting that automically provides results in this order.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 41732182
No.  There is no pre-defined sort order in SQL tables.  The table may appear to be sorted but any number of things can change that if you are not using an ORDER BY clause.  The only ways to get the INSERT order are to use an 'autoincrement' column or to have a DATETIME column that gets populated as you insert rows (usually using a NOW() command).  Those just give ORDER BY something to work with.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question