Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Turning off the automatic Ascending order in MySQL for results

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy to use inventory management software 7 107
mcrypt_create_iv() is deprecated 4 282
mysql disables rename 4 93
mysql date time 14 38
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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