order by problem

This is a discussion on order by problem within the MySQL Database forums, part of the Database Forums category; In a mysql database is a field called 'number' which is actually filled by numbers. In a query I want ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-12-2008
Jannis
 
Posts: n/a
Default order by problem

In a mysql database is a field called 'number' which is actually
filled by numbers. In a query I want my results ordered by number so
the last clause in my query is ORDER BY 'number'. Numbers then are
given in an 'alphabetical'order: first 100 and later e.g. 83 etc. Is
it possible to get numbers like this in such a way that in a sequence
from 1 to 111 the 1 comes first and 111 last? Ordering by id or other
field is no option.
The number field is INT(3).
Thank you,
Onno
Reply With Quote
  #2 (permalink)  
Old 01-12-2008
Charles Polisher
 
Posts: n/a
Default Re: order by problem

On 2008-01-12, Jannis <anne.bosscha@hccnet.nl> wrote:
> In a mysql database is a field called 'number' which is actually
> filled by numbers. In a query I want my results ordered by number so
> the last clause in my query is ORDER BY 'number'. Numbers then are
> given in an 'alphabetical'order: first 100 and later e.g. 83 etc. Is
> it possible to get numbers like this in such a way that in a sequence
> from 1 to 111 the 1 comes first and 111 last? Ordering by id or other
> field is no option.
> The number field is INT(3).
> Thank you,
> Onno


Works for me. I think you are using 'apostrophes'
where you intended `backticks`.

mysql> create table foo (`number` int(3));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into foo set `number` = 10;
Query OK, 1 row affected (0.02 sec)

mysql> insert into foo set `number` = 111;
Query OK, 1 row affected (0.01 sec)

mysql> insert into foo set `number` = 1;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM foo;
+--------+
| number |
+--------+
| 10 |
| 111 |
| 1 |
+--------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM foo ORDER BY `number`;
+--------+
| number |
+--------+
| 1 |
| 10 |
| 111 |
+--------+
3 rows in set (0.00 sec)


Reply With Quote
  #3 (permalink)  
Old 01-13-2008
Michael Austin
 
Posts: n/a
Default Re: order by problem

Charles Polisher wrote:
> On 2008-01-12, Jannis <anne.bosscha@hccnet.nl> wrote:
>> In a mysql database is a field called 'number' which is actually
>> filled by numbers. In a query I want my results ordered by number so
>> the last clause in my query is ORDER BY 'number'. Numbers then are
>> given in an 'alphabetical'order: first 100 and later e.g. 83 etc. Is
>> it possible to get numbers like this in such a way that in a sequence
>> from 1 to 111 the 1 comes first and 111 last? Ordering by id or other
>> field is no option.
>> The number field is INT(3).
>> Thank you,
>> Onno

>
> Works for me. I think you are using 'apostrophes'
> where you intended `backticks`.
>
> mysql> create table foo (`number` int(3));
> Query OK, 0 rows affected (0.04 sec)
>
> mysql> insert into foo set `number` = 10;
> Query OK, 1 row affected (0.02 sec)
>
> mysql> insert into foo set `number` = 111;
> Query OK, 1 row affected (0.01 sec)
>
> mysql> insert into foo set `number` = 1;
> Query OK, 1 row affected (0.01 sec)
>
> mysql> SELECT * FROM foo;
> +--------+
> | number |
> +--------+
> | 10 |
> | 111 |
> | 1 |
> +--------+
> 3 rows in set (0.00 sec)
>
> mysql> SELECT * FROM foo ORDER BY `number`;
> +--------+
> | number |
> +--------+
> | 1 |
> | 10 |
> | 111 |
> +--------+
> 3 rows in set (0.00 sec)
>
>


you should never define a column name using reserved key words - they
only cause you more grief than they are worth!

Reply With Quote
  #4 (permalink)  
Old 01-13-2008
Jannis
 
Posts: n/a
Default Re: order by problem

On Sat, 12 Jan 2008 21:23:38 -0000 wrote Charles Polisher
<cpolish@nonesuch.com>:

>On 2008-01-12, Jannis <anne.bosscha@hccnet.nl> wrote:
>> In a mysql database is a field called 'number' which is actually
>> filled by numbers. In a query I want my results ordered by number so
>> the last clause in my query is ORDER BY 'number'. Numbers then are
>> given in an 'alphabetical'order: first 100 and later e.g. 83 etc. Is
>> it possible to get numbers like this in such a way that in a sequence
>> from 1 to 111 the 1 comes first and 111 last? Ordering by id or other
>> field is no option.
>> The number field is INT(3).
>> Thank you,
>> Onno

>
>Works for me. I think you are using 'apostrophes'
>where you intended `backticks`.
>
>mysql> create table foo (`number` int(3));
>Query OK, 0 rows affected (0.04 sec)
>
>mysql> insert into foo set `number` = 10;
>Query OK, 1 row affected (0.02 sec)
>
>mysql> insert into foo set `number` = 111;
>Query OK, 1 row affected (0.01 sec)
>
>mysql> insert into foo set `number` = 1;
>Query OK, 1 row affected (0.01 sec)
>
>mysql> SELECT * FROM foo;
>+--------+
>| number |
>+--------+
>| 10 |
>| 111 |
>| 1 |
>+--------+
>3 rows in set (0.00 sec)
>
>mysql> SELECT * FROM foo ORDER BY `number`;
>+--------+
>| number |
>+--------+
>| 1 |
>| 10 |
>| 111 |
>+--------+
>3 rows in set (0.00 sec)
>


Actually I used those `backticks`. Sorry about that. Still 2 comes
after 111.
Reply With Quote
  #5 (permalink)  
Old 01-13-2008
Paul Lautman
 
Posts: n/a
Default Re: order by problem

Jannis wrote:
>
> Actually I used those `backticks`. Sorry about that. Still 2 comes
> after 111.


So export the CREATE TABLE statement and a few sample rows of data from
phpMyAdmin and post them here for us to take a look at.




Reply With Quote
  #6 (permalink)  
Old 01-14-2008
Jannis
 
Posts: n/a
Default Re: order by problem

On Sun, 13 Jan 2008 13:36:20 -0000 wrote "Paul Lautman"
<paul.lautman@btinternet.com>:

>Jannis wrote:
>>
>> Actually I used those `backticks`. Sorry about that. Still 2 comes
>> after 111.

>
>So export the CREATE TABLE statement and a few sample rows of data from
>phpMyAdmin and post them here for us to take a look at.
>
>
>

The system works as long as I'm working in mySQL. After copying tot
PHP the problem occurs. I did not expect this. Maybe I posted this in
the wrong group. Sorry.
As for using names. Actually I gave the fields names in Dutch, so
there are no problems about reserved terms.
Reply With Quote
  #7 (permalink)  
Old 01-14-2008
Captain Paralytic
 
Posts: n/a
Default Re: order by problem

On 14 Jan, 10:49, Jannis <anne.boss...@hccnet.nl> wrote:
> On Sun, 13 Jan 2008 13:36:20 -0000 wrote "Paul Lautman"
> <paul.laut...@btinternet.com>:
>
> >Jannis wrote:

>
> >> Actually I used those `backticks`. Sorry about that. Still 2 comes
> >> after 111.

>
> >So export the CREATE TABLE statement and a few sample rows of data from
> >phpMyAdmin and post them here for us to take a look at.

>
> The system works as long as I'm working in mySQL. After copying tot
> PHP the problem occurs. I did not expect this. Maybe I posted this in
> the wrong group. Sorry.
> As for using names. Actually I gave the fields names in Dutch, so
> there are no problems about reserved terms.


I don't understand what you mean by "copying tot (SIC) PHP"

If you are running a query, you are using MySQL.
Reply With Quote
  #8 (permalink)  
Old 01-14-2008
Jannis
 
Posts: n/a
Default Re: order by problem

On Mon, 14 Jan 2008 03:13:46 -0800 (PST) wrote Captain Paralytic
<paul_lautman@yahoo.com>:

>On 14 Jan, 10:49, Jannis <anne.boss...@hccnet.nl> wrote:
>> On Sun, 13 Jan 2008 13:36:20 -0000 wrote "Paul Lautman"
>> <paul.laut...@btinternet.com>:
>>
>> >Jannis wrote:

>>
>> >> Actually I used those `backticks`. Sorry about that. Still 2 comes
>> >> after 111.

>>
>> >So export the CREATE TABLE statement and a few sample rows of data from
>> >phpMyAdmin and post them here for us to take a look at.

>>
>> The system works as long as I'm working in mySQL. After copying tot
>> PHP the problem occurs. I did not expect this. Maybe I posted this in
>> the wrong group. Sorry.
>> As for using names. Actually I gave the fields names in Dutch, so
>> there are no problems about reserved terms.

>
>I don't understand what you mean by "copying tot (SIC) PHP"
>
>If you are running a query, you are using MySQL.


I mean
<?php $sql="(the sql query)"; ?>
and of course what is needed more to read the results.
Reply With Quote
  #9 (permalink)  
Old 01-14-2008
Rik Wasmus
 
Posts: n/a
Default Re: order by problem

On Mon, 14 Jan 2008 12:49:33 +0100, Jannis <anne.bosscha@hccnet.nl> wrote:
> On Mon, 14 Jan 2008 03:13:46 -0800 (PST) wrote Captain Paralytic
> <paul_lautman@yahoo.com>:
>> On 14 Jan, 10:49, Jannis <anne.boss...@hccnet.nl> wrote:
>>> The system works as long as I'm working in mySQL. After copying tot
>>> PHP the problem occurs. I did not expect this. Maybe I posted this in
>>> the wrong group. Sorry.


>> If you are running a query, you are using MySQL.


> I mean
> <?php $sql="(the sql query)"; ?>
> and of course what is needed more to read the results.


Simply put: if the query works in MySQL, then it will work identically if
either PHP or any other client sends it to MySQL. Ergo: your query in PHP
is NOT exactly the same, so print/echo it out, and find the difference.
--
Rik Wasmus
Reply With Quote
  #10 (permalink)  
Old 01-14-2008
Jannis
 
Posts: n/a
Default Re: order by problem

On Mon, 14 Jan 2008 12:57:45 +0100 wrote "Rik Wasmus"
<luiheidsgoeroe@hotmail.com>:

>On Mon, 14 Jan 2008 12:49:33 +0100, Jannis <anne.bosscha@hccnet.nl> wrote:
>> On Mon, 14 Jan 2008 03:13:46 -0800 (PST) wrote Captain Paralytic
>> <paul_lautman@yahoo.com>:
>>> On 14 Jan, 10:49, Jannis <anne.boss...@hccnet.nl> wrote:
>>>> The system works as long as I'm working in mySQL. After copying tot
>>>> PHP the problem occurs. I did not expect this. Maybe I posted this in
>>>> the wrong group. Sorry.

>
>>> If you are running a query, you are using MySQL.

>
>> I mean
>> <?php $sql="(the sql query)"; ?>
>> and of course what is needed more to read the results.

>
>Simply put: if the query works in MySQL, then it will work identically if
>either PHP or any other client sends it to MySQL. Ergo: your query in PHP
>is NOT exactly the same, so print/echo it out, and find the difference.


I did so. The difference is that I concatenated two columns in order
to get e.g. 3 and 3A in the same column. I expected the same behavior
for the new column, but I forgot to test that. As you know this new
column has to get a new name and this is ordered alphabetically even
in SQL.
For me it is a surprise, but maybe that is lack of experience.
Thank for your help.
Jannis
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:49 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0