Can I change row order in MySQL Database?

Post here if you need help with SQL.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Can I change row order in MySQL Database?

Post by nyo »

Hi,

I have a simple MySQL database and I am working on it through phpMyAdmin. I have a couple of rows and I was wondering if it is possible to change the order of the rows.

Thanks.
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Can I change row order in MySQL Database?

Post by libeco »

In what way? When selecting? Or just the id?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Can I change row order in MySQL Database?

Post by jacek »

By default the rows are ordered by the ID so to reorder them you can change that.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Can I change row order in MySQL Database?

Post by nyo »

Well, the table has 5 rows in the following order now:

ID
content
title
name
date


So, can I change this order to have something like:

ID
title
name
content
date


without deleting and recreating them? Not so important but just wondering.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Can I change row order in MySQL Database?

Post by jacek »

Those are columns ... and as far as I know there is no war to change the order
Image
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Can I change row order in MySQL Database?

Post by libeco »

Download HeidiSQL (assuming you're using Windows). It's a free database management application (I use it a lot and really like it). It supports re-ordering columns.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Can I change row order in MySQL Database?

Post by nyo »

jacek wrote:Those are columns ... and as far as I know there is no war to change the order
Sorry, my bad, I should say columns and thanks for your opinion.

libeco, thanks for the suggestion, never heard of that application before but it seems nice. I will definitely try it on my localhost, but my hosting account has phpmyadmin and I have to use it.
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Can I change row order in MySQL Database?

Post by libeco »

phpmyadmin is just an interface. If your host supports remote connections you can still use HeidiSQL.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Can I change row order in MySQL Database?

Post by nyo »

libeco wrote:phpmyadmin is just an interface. If your host supports remote connections you can still use HeidiSQL.
I downloaded HeidiSQL and using it on localhost. Do you know how to import a database? It has "Import > Load SQL File" option but that doesn't import the database to my local mysql server.
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Can I change row order in MySQL Database?

Post by libeco »

If you hit the Execute SQL (F9) button it will execute the SQL.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Can I change row order in MySQL Database?

Post by nyo »

libeco wrote:If you hit the Execute SQL (F9) button it will execute the SQL.
Thank you libeco, it's ok now. But it seems it doesn't directly import a database. It requires a database to be created and selected so that it can import the SQL query and populate the database.
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Can I change row order in MySQL Database?

Post by libeco »

I tested it this afternoon and it worked for me. Make sure there's a create statement if you want the database created. Here's my test script if you want to try:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

-- 
-- Database: `test123`
-- 
CREATE DATABASE `test123` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `test123`;

CREATE TABLE `bla` (
  `asd` varchar(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
shahzadkb
Posts: 2
Joined: Fri Aug 19, 2011 2:45 pm

Re: Can I change row order in MySQL Database?

Post by shahzadkb »

Databases do not store data in any particular order, it is up to you to write your select statements in such a way the the data is retrieved in the order you want.
My aim is to put down on paper what I see and what I feel in the best and simplest way.
Post Reply