Jump to content

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
https://forums.phpfreaks.com/topic/77734-whats-the-difference/
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
https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-393495
Share on other sites

thanks.. theres actually nothing wrong with my query, its just the HeidiSql has some bug or something which results to 0 row, but when I tried it on phpmyadmin it fetch the right rows..

 

 

what could be the problem in heidisql? has anyone encountered a problem like this?

Link to comment
https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-393514
Share on other sites

thats pimp barand, thx. never thought sub query is allowed in ORDER BY statement.. can you lead me to a tutorial that teach this? I've try searching many tutorials and none of them give me atleast a sample of this kind of query..

 

where else does the subquery allowed?

 

thx

Link to comment
https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394280
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
https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394465
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.