sorting problem
Moderator: General Moderators
sorting problem
I have a database where it stores brand names:
Adidas
Puma
Nike
Reebok
Diadora
the column name is brand
How can I display the brand name in order of the names ?
click on adidas it will list all items but adidas first
click on puma it will list all items but puma first
Can this be done ? I know it can be done by listing just adidas or puma etc but i need all records displaying but in order of brand name ?
Any ideas ??
Adidas
Puma
Nike
Reebok
Diadora
the column name is brand
How can I display the brand name in order of the names ?
click on adidas it will list all items but adidas first
click on puma it will list all items but puma first
Can this be done ? I know it can be done by listing just adidas or puma etc but i need all records displaying but in order of brand name ?
Any ideas ??
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
Well if you read my question you will notice that i dont want to do that - I`m wanting to sort depending on the brand name.
click on adidas it will display ALL records starting with adidas
click on puma it will display ALL records starting with puma
click on nike it will display ALL records starting with nike
not
select * from table order by brand

click on adidas it will display ALL records starting with adidas
click on puma it will display ALL records starting with puma
click on nike it will display ALL records starting with nike
not
select * from table order by brand
I would tend to approach this with two different selects: first for the brand selected and second for everything except the brand selected. I'm not sure what the UNION operator does to the sort order, but you could try:
but I expect the following would work even if the above doesn't:
Code: Select all
select * from table
where brand = 'Puma'
UNION
select * from table
where brand != 'Puma' ;Code: Select all
(select '1' sort_order, * from table
where brand = 'Puma')
UNION
(select '2' sort_order, * from table
where brand != 'Puma')
order by sort_order ;- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The only problem with UNION is that if you're using MySQL it's only been implemented since version 4:
http://www.mysql.com/doc/en/UNION.html
If you're using MySQL version 3 then this may be of interest:
http://www.nstep.net/~mpbailey/programm ... .union.php
But otherwise, the simple solution is to do two SELECTS as spammich stated and put the ORDER BY statement in the second one:
then
and if you're using a database that can do UNIONs then you can just do that.
Mac
http://www.mysql.com/doc/en/UNION.html
If you're using MySQL version 3 then this may be of interest:
http://www.nstep.net/~mpbailey/programm ... .union.php
But otherwise, the simple solution is to do two SELECTS as spammich stated and put the ORDER BY statement in the second one:
Code: Select all
SELECT field1, field2, field3 FROM table WHERE brand='puma'Code: Select all
SELECT field1, field2, field3 FROM table WHERE brand <> 'puma' ORDER BY brandMac