Author Topic: whats the difference??  (Read 670 times)

0 Members and 1 Guest are viewing this topic.

Offline DanDaBeginnerTopic starter

  • Enthusiast
  • Posts: 211
    • View Profile
whats the difference??
« on: November 17, 2007, 10:54:29 AM »
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

Code: [Select]
(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..

Code: [Select]
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
« Last Edit: November 17, 2007, 10:56:24 AM by DanDaBeginner »

Offline rajivgonsalves

  • Addict
  • Posts: 2,160
  • Gender: Male
  • Whizzkid
    • View Profile
    • My Personal Website
Re: whats the difference??
« Reply #1 on: November 17, 2007, 11:39:17 AM »
should be

Code: [Select]
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
Cheers!
Rajiv

Code: [Select]
// commenting code... nah!!! if it was hard to write, it should be hard to read :P
echo implode('', array_map('chr', explode(',','87,104,105,122,122,107,105,100')));

Offline DanDaBeginnerTopic starter

  • Enthusiast
  • Posts: 211
    • View Profile
Re: whats the difference??
« Reply #2 on: November 17, 2007, 12:21:16 PM »
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?

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: whats the difference??
« Reply #3 on: November 17, 2007, 08:48:45 PM »
try
Code: [Select]
SELECT `country_name` FROM `country`
ORDER BY `country_name` IN ('UNITED STATES','CANADA') DESC, `country_name`
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline DanDaBeginnerTopic starter

  • Enthusiast
  • Posts: 211
    • View Profile
Re: whats the difference??
« Reply #4 on: November 19, 2007, 12:48:04 AM »
thats pimp barand, thanks. 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?

thanks

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: whats the difference??
« Reply #5 on: November 19, 2007, 02:54:01 AM »
It isn't a subquery, it's just a Boolean expression returning 1 or 0 (true or false)
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline DanDaBeginnerTopic starter

  • Enthusiast
  • Posts: 211
    • View Profile
Re: whats the difference??
« Reply #6 on: November 19, 2007, 07:21:30 AM »
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..

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: whats the difference??
« Reply #7 on: November 19, 2007, 08:59:47 AM »
try
Code: [Select]
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.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: whats the difference??
« Reply #8 on: November 19, 2007, 01:57:55 PM »
.. 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.
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns