Solved

Turning off the automatic Ascending order in MySQL for results

Posted on 2016-07-27
4
24 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 40

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now