Bluehost.com Web Hosting $6.95

Returning values which don't match values in table

This is a discussion on Returning values which don't match values in table within the MySQL Database forums, part of the Database Forums category; Hi, I hope that the subject isn't too vague... I have a table with country codes and names and ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-10-2007
christoph lauterbach
 
Posts: n/a
Default Returning values which don't match values in table

Hi,

I hope that the subject isn't too vague...

I have a table with country codes and names and want to query against
it with a list of codes. The query should return the values, which are
not in the table.

Example:
The table contains: 'de', 'fr', 'it', 'at', 'ch', 'es'
The queried values are: 'de', 'it', 'es', 'pl', 'cz', 'us'
The query should return: 'pl', 'cz', 'us'

I know that there is a way with a subselect, but i don't know how to
fill it with the query values.

Thanks a lot for your help
Christoph

Reply With Quote
  #2 (permalink)  
Old 02-10-2007
Paul Lautman
 
Posts: n/a
Default Re: Returning values which don't match values in table

christoph lauterbach wrote:
> Hi,
>
> I hope that the subject isn't too vague...
>
> I have a table with country codes and names and want to query against
> it with a list of codes. The query should return the values, which are
> not in the table.
>
> Example:
> The table contains: 'de', 'fr', 'it', 'at', 'ch', 'es'
> The queried values are: 'de', 'it', 'es', 'pl', 'cz', 'us'
> The query should return: 'pl', 'cz', 'us'
>
> I know that there is a way with a subselect, but i don't know how to
> fill it with the query values.
>
> Thanks a lot for your help
> Christoph


You haven't told us a great deal about the tables so in my query I have made
the following assumptions:
1) The table is called country_code_table
2) The field within it holding the country codes is called country_code

If your table/field names differ then you can change them accordingly.
Here is the query:
SELECT `q`.`country_code`
FROM (
SELECT 'de' `country_code`
UNION
SELECT 'it'
UNION
SELECT 'es'
UNION
SELECT 'pl'
UNION
SELECT 'cz'
UNION
SELECT 'us'
) AS `q`
LEFT JOIN `country_code_table` `c` USING(`country_code`)
WHERE `c`.`country_code` is NULL


Reply With Quote
  #3 (permalink)  
Old 02-11-2007
christoph lauterbach
 
Posts: n/a
Default Re: Returning values which don't match values in table

> You haven't told us a great deal about the tables so in my query I have made
> the following assumptions:
> 1) The table is called country_code_table
> 2) The field within it holding the country codes is called country_code
>
> If your table/field names differ then you can change them accordingly.
> Here is the query:
> SELECT `q`.`country_code`
> FROM (
> SELECT 'de' `country_code`
> UNION
> SELECT 'it'
> UNION
> SELECT 'es'
> UNION
> SELECT 'pl'
> UNION
> SELECT 'cz'
> UNION
> SELECT 'us'
> ) AS `q`
> LEFT JOIN `country_code_table` `c` USING(`country_code`)
> WHERE `c`.`country_code` is NULL


Thanks a lot Paul, this is great! I guess performance must be very
good with this...

Regards
Christoph

Reply With Quote
  #4 (permalink)  
Old 02-11-2007
christoph lauterbach
 
Posts: n/a
Default Re: Returning values which don't match values in table

Okay, this evening I mastered to implement Pauls solution. The correct
query reads as follows:
SELECT `q`.`country_code`
FROM (
SELECT 'de' AS `country_code`
UNION
SELECT 'it' AS `country_code`
UNION
SELECT 'es' AS `country_code`
UNION
SELECT 'pl' AS `country_code`
UNION
SELECT 'cz' AS `country_code`
UNION
SELECT 'us' AS `country_code`
) AS `q`
LEFT JOIN `country_code_table` USING(`country_code`)
WHERE `country_code_table`.`country_code` is NULL

Regards
Christoph

Reply With Quote
  #5 (permalink)  
Old 02-11-2007
Paul Lautman
 
Posts: n/a
Default Re: Returning values which don't match values in table

christoph lauterbach wrote:
> Okay, this evening I mastered to implement Pauls solution. The correct
> query reads as follows:
> SELECT `q`.`country_code`
> FROM (
> SELECT 'de' AS `country_code`
> UNION
> SELECT 'it' AS `country_code`
> UNION
> SELECT 'es' AS `country_code`
> UNION
> SELECT 'pl' AS `country_code`
> UNION
> SELECT 'cz' AS `country_code`
> UNION
> SELECT 'us' AS `country_code`
> ) AS `q`
> LEFT JOIN `country_code_table` USING(`country_code`)
> WHERE `country_code_table`.`country_code` is NULL
>
> Regards
> Christoph


You only need the column name on the first sub-select ('de' in this case)
And using table aliases tend to make for easier reading (and also are
necessary for self joins).
I did test the query that I posted, so what were the changes that youfound
necessary?


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0