Jump to content

whats the difference??


DanDaBeginner

Recommended Posts

im trying to select a country but with USA and CANADA first and others to be arranged alphabetically, so I have this query.

 

but the problem is when used this query with both select statement enclosed in brackets theres no row selected

 

(SELECT `country_name` FROM `country` as t2
WHERE `country_name` IN ('UNITED STATES','CANADA') )
UNION
( SELECT `country_name` FROM `country` as t3
WHERE `country_name` NOT IN ('UNITED STATES','CANADA') 
ORDER BY `country_name` ASC )

 

but this one, first select statement without bracket it displays what I wanted..

 

SELECT `country_name` FROM `country` as t2
WHERE `country_name` IN ('UNITED STATES','CANADA')
UNION
( SELECT `country_name` FROM `country` as t3
WHERE `country_name` NOT IN ('UNITED STATES','CANADA') 
ORDER BY `country_name` ASC )

 

what is the difference between the two? and why the first query doesn't display any row?

 

thanks in advance

Link to comment
Share on other sites

should be

 

SELECT `country_name` FROM `country`
WHERE `country_name` IN ('UNITED STATES','CANADA')
UNION
( SELECT `country_name` FROM `country`
WHERE `country_name` NOT IN ('UNITED STATES','CANADA') 
ORDER BY `country_name` ASC )

 

the as in the from clause maybe causing the problem it should be a alias so `country` t3 or `country` t3 as per my knowlegde the "as'' keyword I have used when selecting fields not the tables

Link to comment
Share on other sites

try

SELECT `country_name` FROM `country`
ORDER BY `country_name` IN ('UNITED STATES','CANADA') DESC, `country_name`

I've always used "ORDER BY FIELD( myField, ...options-in-reverse-order... ) DESC, myField", because it's more robust -- alphabetical isn't always desirable.  The IN clause just a boolean.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.