Solved

Turning off the automatic Ascending order in MySQL for results

Posted on 2016-07-27
4
22 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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

20 Experts available now in Live!

Get 1:1 Help Now