DanDaBeginner Posted November 17, 2007 Share Posted November 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/ Share on other sites More sharing options...
rajivgonsalves Posted November 17, 2007 Share Posted November 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-393495 Share on other sites More sharing options...
DanDaBeginner Posted November 17, 2007 Author Share Posted November 17, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-393514 Share on other sites More sharing options...
Barand Posted November 18, 2007 Share Posted November 18, 2007 try SELECT `country_name` FROM `country` ORDER BY `country_name` IN ('UNITED STATES','CANADA') DESC, `country_name` Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-393703 Share on other sites More sharing options...
DanDaBeginner Posted November 19, 2007 Author Share Posted November 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394280 Share on other sites More sharing options...
Barand Posted November 19, 2007 Share Posted November 19, 2007 It isn't a subquery, it's just a Boolean expression returning 1 or 0 (true or false) Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394327 Share on other sites More sharing options...
DanDaBeginner Posted November 19, 2007 Author Share Posted November 19, 2007 ooopss, did I say subquery?? I mean, never thought you can put IN statement on ORDER BY.. can you lead me to a tutorial that does this? been searchin for a tutorial but never found that does this.. Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394404 Share on other sites More sharing options...
fenway Posted November 19, 2007 Share Posted November 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394465 Share on other sites More sharing options...
Barand Posted November 19, 2007 Share Posted November 19, 2007 .. can you lead me to a tutorial that does this? been searchin for a tutorial but never found that does this.. Afraid I haven't seen one either. Quote Link to comment https://forums.phpfreaks.com/topic/77734-whats-the-difference/#findComment-394642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.