Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

ORDER BY two columns ?

 
   Database Help (Home) -> mySQL RSS
Next:  ASE 12.5 on Linux  
Author Message
usenet

External


Since: Jun 15, 2004
Posts: 10



(Msg. 1) Posted: Thu Feb 10, 2005 6:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: mailing>database>mysql (more info?)

this post is not archived.

 >> Stay informed about: ORDER BY two columns ? 
Back to top
Login to vote
usenet

External


Since: Jun 15, 2004
Posts: 10



(Msg. 2) Posted: Thu Feb 10, 2005 6:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

this post is not archived.

 >> Stay informed about: ORDER BY two columns ? 
Back to top
Login to vote
usenet

External


Since: Jun 15, 2004
Posts: 10



(Msg. 3) Posted: Thu Feb 10, 2005 6:40 pm
Post subject: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
Chris Hope

External


Since: Mar 26, 2004
Posts: 32



(Msg. 4) Posted: Thu Feb 10, 2005 6:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

usenet.DeleteThis@isotopeREEMOOVEmedia.com wrote:

 > I'm trying to figure out how I can sort records alphabetically by
 > almost "blending" two fields.
 >
 > SELECT city, county
 > FROM fullist
 > WHERE type = 'city' or type = 'county'
 > ORDER BY ???;
 >
 > It should output like this :
 >
 > ----------------------------
 > | city | county | type |
 > ----------------------------
 > | A | x | city |
 > | x | B | county |
 > | C | x | city |
 > | x | D | county |
 > ----------------------------
 >
 > "Type" indicates which of the fields should be used for the sort. I'm
 > thinking that a JOIN might be necessary, but can't seem to get my head
 > around how to do it.
 >
 > Any and all advice will be greatly appreciated !

If it will only ever have text for the city OR the country you could do
this:

SELECT if(city <> '', city, country) as citycountry, type
FROM fullist
WHERE type = 'city' or type = 'country'
ORDER BY citycountry;

However, if this is the case (ie you are only storing the city or the
country in the row) then you may be better off to have the data in two
separate tables.

--
Chris Hope - The Electric Toolbox - <a rel="nofollow" style='text-decoration: none;' href="http://www.electrictoolbox.com/" target="_blank">http://www.electrictoolbox.com/</a>
 >> Stay informed about: ORDER BY two columns ? 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 5) Posted: Thu Feb 10, 2005 6:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

usenet DeleteThis @isotopeREEMOOVEmedia.com wrote:
 > I agree with the general idea of what you suggested : I think I need to create
 > a third field in the query and assign it the value of either 'city' or 'state'
 > depeding on the value of 'type.' But apparently that's beyond my skills so far.
 > <g>

I don't believe it's necessary to have another field in your
select-list. You can use expressions in the ORDER BY clause.
Does this do what you want?

SELECT city, county
FROM fullist
WHERE type = 'city' or type = 'county'
ORDER BY IF(type = 'city', city, country);

Regards,
Bill K.
 >> Stay informed about: ORDER BY two columns ? 
Back to top
Login to vote
Chris Hope

External


Since: Mar 26, 2004
Posts: 32



(Msg. 6) Posted: Thu Feb 10, 2005 7:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bill Karwin wrote:


  >> I agree with the general idea of what you suggested : I think I need
  >> to create a third field in the query and assign it the value of
  >> either 'city' or 'state'
  >> depeding on the value of 'type.' But apparently that's beyond my
  >> skills so far. <g>
 >
 > I don't believe it's necessary to have another field in your
 > select-list. You can use expressions in the ORDER BY clause.
 > Does this do what you want?
 >
 > SELECT city, county
 > FROM fullist
 > WHERE type = 'city' or type = 'county'
 > ORDER BY IF(type = 'city', city, country);

Nice solution.

--
Chris Hope - The Electric Toolbox - <a rel="nofollow" style='text-decoration: none;' href="http://www.electrictoolbox.com/" target="_blank">http://www.electrictoolbox.com/</a>
 >> Stay informed about: ORDER BY two columns ? 
Back to top
Login to vote
usenet

External


Since: Jun 15, 2004
Posts: 10



(Msg. 7) Posted: Thu Feb 10, 2005 8:40 pm
Post subject: Re: ORDER BY two columns ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
Display posts from previous:   
Related Topics:
PLS HELP! - show databases and tables and columns - Hi, I just wonder if someone can help me with this: I need to create a sql script which will run when user installs/upgrades my app. User may already have the database and tables tructure setup on the server, or may not. The script needs to..

Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...

login as user 'root' but do not have root privlages and my.. - Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via..

FLUSH TABLES hangs if table is locked - Using FLUSH TABLES via the C query API mysql_query() hangs if the table is locked already. That is to say, nothing prevents me from running a LOCK TABLES twice; it won't tell me "it's already locked, don't try to run a FLUSH". Anyone know ...
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]