-4

That is a table in mysql, the 010 is expected to be the last on the table. How do I handle that? The database automatically sort the values entered which id not needed 1

dur
  • 15,689
  • 25
  • 79
  • 125

3 Answers3

1

What you're asking for is basically "show the rows in the sequence entered," which (strictly-speaking) is undefined in SQL.

What I usually do with my tables is add an ID column like:

create table foo (id int not null primary key auto_increment, ...rest of columns...)

This way, the rows are sortable by sequence-of-entry. It doesn't have to be the primary key, either--any auto_increment column will work. (auto_increment is a MySqlism, btw, other RDBMSes have other syntax to do basically the same thing).

BTW, your column is, strictly speaking, sorting correctly--looks like it's a char or varchar column, so it's sorting in lexicographical order rather than numeric. If you want to port your data over, I expect you can do something like:

insert into new_table
select * from old_table order by CAST(Visitorsid AS UNSIGNED)

At least I think that works...don't have a MySQL command line handy to try it :-). Good luck!

(Updated to fix MySQL syntax and use the right column name)

BJ Black
  • 2,483
  • 9
  • 15
0

Database tables are unsorted sets, and have no intrinsic order. If you want to get the rows in a specific order, you'll have to specify it explicitly, with an order by clause:

SELECT   *
FROM     mytable
ORDER BY CAST(visitorsid AS INTEGER)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    I think what's happening here is that phpmyadmin is automagically slapping in an ORDER BY $col1, $col2... under the covers here. If OP were to use your query instead of phpmyadmin's default what-you-get-when-you-click-browse-table, he'd get the correct result. – BJ Black Aug 19 '16 at 22:00
0

You could specify the order of a character field by its length, instead of standard collation order.

SELECT * FROM MY_TABLE ORDER BY LENGTH(visitorid);

I should add that by the leading 0 I assume this is not a numeric field.

RudolphEst
  • 1,240
  • 13
  • 21