tags:

views:

29

answers:

1

Hi, guys,

An abstraction of the problem is like this:

I have one table having a column called 'country'. the value stored are name of the country, e.g. US, UK..

I have another table having a column called 'country_code'. the value stored are numerical representations of the country, e.g. 12, 17...

how can I perform a join operation (e.g. inner join) based on these 2 tables? the difficulty is that the country and country_code has a one-to-one mapping but not directly equal to each other.

A: 

You could create a Mapping table containging the country and the country_code.

I assume you cannot change the table containing the country_code to use the string representation from country, or add an int column to your countries table?

Something like

country_mappings

  • country varchar column
  • country_code int column
  • PRIMARY KEY country, country_mapping

'

SELECT  *
FROM    countries c INNER JOIN
        country_mappings cm ON c.country = cm.country inner join
        your_other_table yot ON cm.country_code = yot.country_code
astander
That'd be a one-to-one relationship between `COUNTRY_MAPPINGS` and `COUNTRIES`...
OMG Ponies
This is correct. As mentioned in the answer, I am not sure if the OP can change the structure of the original 2 tables. If so, it would make this a lot easier, and no mapping table would be required.
astander
Could use a CASE statement to add the country onto the existing table
OMG Ponies
If you are refering to using a case statement in the query, this could be a **very big** case statement. I think that hard coding something like that might not always be the way to go. have a look at http://www.listofcountriesoftheworld.com/
astander
Could put it in a view. Bad data models mean hacks :)
OMG Ponies
If the original country_code entry is not from the OP system, and their are possibly more, then a view would have to be craeted per object. Where as the table can be used with a source column, to distingish between various sorces. **I aggree, this should have been done souly between the 2 tables in question.**
astander