Unix Technical Forum

Sorting MySQL queries

This is a discussion on Sorting MySQL queries within the MySQL General forum forums, part of the MySQL category; --> I have a list of subjects, such as "Linux", "Open Source", and "the World Wide Web". The subjects are ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:09 AM
Dotan Cohen
 
Posts: n/a
Default Sorting MySQL queries

I have a list of subjects, such as "Linux", "Open Source", and "the
World Wide Web". The subjects are stored in MySQL and being retrieved
via php. I currently organize them alphabetically with MySQL's "ORDER
BY ASC" argument, however, if there is a preceding "the " or "a " then
that is considered as part of the alphabetical order. Thus, all the
subjects starting with "the " are grouped together, as are the
subjects starting with "a ". How can I order by ascending, without
taking the preceding "the " or "a " into account?

** Example:
Now, the list is ordeded like this:
a Distribution
a Text Editor
a Virus
Bluetooth
Copyleft
DRM
Fedora
Firefox

However, I'd like it to be ordered like this:
Bluetooth
Copyleft
a Distribution
DRM
Fedora
Firefox
a Text Editor
a Virus

Current code:
$query = "SELECT subject FROM table ORDER BY subject asc";
$result = mysql_query($query);


Thanks in advance.

Dotan Cohen

http://what-is-what.com/what_is/world_wide_web.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:09 AM
Christian Hammers
 
Posts: n/a
Default Re: Sorting MySQL queries



On 2006-11-06 Dotan Cohen wrote:
> I have a list of subjects, such as "Linux", "Open Source", and "the
> World Wide Web". The subjects are stored in MySQL and being retrieved
> via php. I currently organize them alphabetically with MySQL's "ORDER
> BY ASC" argument, however, if there is a preceding "the " or "a " then
> that is considered as part of the alphabetical order. Thus, all the
> subjects starting with "the " are grouped together, as are the
> subjects starting with "a ". How can I order by ascending, without
> taking the preceding "the " or "a " into account?


Make a second column that only contains
ALTER TABLE table ADD cooked_subject;
UPDATE table SET cooked_subject = ereg_replace('^(a|the) ', '', subject);
(I don't know how the regular expression function was called exactly but you get the idea)

SELECT subject FROM table ORDER BY cooked_subject;

Of course you could also put the regular expression in the SELECT but that would be slower.



bye,

-christian-

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 05:09 AM
Dotan Cohen
 
Posts: n/a
Default Re: Sorting MySQL queries

On 06/11/06, Christian Hammers <ch@lathspell.de> wrote:
> On 2006-11-06 Dotan Cohen wrote:
> > I have a list of subjects, such as "Linux", "Open Source", and "the
> > World Wide Web". The subjects are stored in MySQL and being retrieved
> > via php. I currently organize them alphabetically with MySQL's "ORDER
> > BY ASC" argument, however, if there is a preceding "the " or "a " then
> > that is considered as part of the alphabetical order. Thus, all the
> > subjects starting with "the " are grouped together, as are the
> > subjects starting with "a ". How can I order by ascending, without
> > taking the preceding "the " or "a " into account?

>
> Make a second column that only contains
> ALTER TABLE table ADD cooked_subject;
> UPDATE table SET cooked_subject = ereg_replace('^(a|the) ', '', subject);
> (I don't know how the regular expression function was called exactly but you get the idea)
>
> SELECT subject FROM table ORDER BY cooked_subject;
>
> Of course you could also put the regular expression in the SELECT but that would be slower.


Would it really slow it down that much? I'll consider the
cooked_subject idea then, if noone has any other suggestions.

Thanks.

Dotan Cohen

http://www.lyricslist.com/lyrics/art...64/beatles.php
http://gmail-com.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 05:09 AM
Christian Hammers
 
Posts: n/a
Default Re: Sorting MySQL queries



On 2006-11-06 Dotan Cohen wrote:
> > Make a second column that only contains
> > ALTER TABLE table ADD cooked_subject;
> > UPDATE table SET cooked_subject = ereg_replace('^(a|the) ', '', subject);
> > (I don't know how the regular expression function was called exactly but you get the idea)
> >
> > SELECT subject FROM table ORDER BY cooked_subject;
> >
> > Of course you could also put the regular expression in the SELECT but that would be slower.

>
> Would it really slow it down that much?


"slow" always depends.. if the table has only 200 rows and the query is run
twice a day, don't care about speed

-christian-
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:34 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com