Unix Technical Forum

How to match two columns in a table with a column in another table

This is a discussion on How to match two columns in a table with a column in another table within the Sybase forums, part of the Database Server Software category; --> Hello How to match two columns in a table with a column in another table. I am using Sybase ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 06:00 PM
BJ
 
Posts: n/a
Default How to match two columns in a table with a column in another table

Hello

How to match two columns in a table with a column in another table. I
am using Sybase server. It has two tables - Customer and Country

Customer table stores customer information and country table is just a
matching table.

Customer Table has:
customer_id
firstname
lastname
place_of_born (i.e country_code)
retirement_country (i.e country_code)

Country Table has:
country_code (e.g. Aus)
country_description (e.g Australia)

My idea is showing the country_description of place_of_born and
retirement_country instead of country_code after sql query. My expect
result is

1234
John
Cornor
Australia
America

instead of

1234
John
Cornor
Aus
US

Pleaes give me some suggestion on writing the SQL statement.

Thanks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 06:00 PM
Michael Peppler
 
Posts: n/a
Default Re: How to match two columns in a table with a column in another table

On Sun, 31 Jul 2005 08:54:52 -0700, BJ wrote:

> Hello
>
> How to match two columns in a table with a column in another table. I am
> using Sybase server. It has two tables - Customer and Country
>
> Customer table stores customer information and country table is just a
> matching table.
>
> Customer Table has:
> customer_id
> firstname
> lastname
> place_of_born (i.e country_code)
> retirement_country (i.e country_code)
>
> Country Table has:
> country_code (e.g. Aus)
> country_description (e.g Australia)
>
> My idea is showing the country_description of place_of_born and
> retirement_country instead of country_code after sql query. My expect
> result is


It's a simple join:


select cs.firstname
, cs.lastname
, c1.country_description
, c2.country_description
from customer cs
, country c1
, country c2
where cs.place_born = c1.country_code
and cs.retirement_country = c2.country_code

should return the information you want.

Michael
--
Michael Peppler [TeamSybase] mpeppler@peppler.org - http://www.peppler.org/
Sybase DBA/Developer
Sybase on Linux FAQ: http://www.peppler.org/FAQ/linux.html


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 06:00 PM
BJ
 
Posts: n/a
Default Re: How to match two columns in a table with a column in another table

I did it.

thanks Michael

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 06:01 PM
Rob Verschoor
 
Posts: n/a
Default Re: How to match two columns in a table with a column in another table

What you need is a 'join' -- like this:

select customer_id, firstname, lastname, born=c1.country_description,
retired=c2.country_description
from Customer c, Country c1, Country c2
where c.place_of_born = c1.country_code
and c.retirement_country = c2.country_code


I recommend reading some basic documentation about SQL since 'joins' are a
rather basic element of SQL (for example, go to
http://sybooks.sybase.com/as.html and pick up a copy of the 'Transact SQL
USer's Guide').

HTH,

Rob
-------------------------------------------------------------
Rob Verschoor

Certified Sybase Professional DBA for ASE 12.5/12.0/11.5/11.0
and Replication Server 12.5 / TeamSybase

Author of Sybase books (order online at www.sypron.nl/shop):
"Tips, Tricks & Recipes for Sybase ASE"
"The Complete Sybase Replication Server Quick Reference Guide"
"The Complete Sybase ASE Quick Reference Guide"

mailto:rob@YOUR.SPAM.sypron.nl.NOT.FOR.ME
http://www.sypron.nl
Sypron B.V., P.O.Box 10695, 2501HR Den Haag, The Netherlands
-------------------------------------------------------------

"BJ" <bonnie.tangyn@gmail.com> wrote in message
news:1122825292.490050.28500@f14g2000cwb.googlegro ups.com...
> Hello
>
> How to match two columns in a table with a column in another table. I
> am using Sybase server. It has two tables - Customer and Country
>
> Customer table stores customer information and country table is just a
> matching table.
>
> Customer Table has:
> customer_id
> firstname
> lastname
> place_of_born (i.e country_code)
> retirement_country (i.e country_code)
>
> Country Table has:
> country_code (e.g. Aus)
> country_description (e.g Australia)
>
> My idea is showing the country_description of place_of_born and
> retirement_country instead of country_code after sql query. My expect
> result is
>
> 1234
> John
> Cornor
> Australia
> America
>
> instead of
>
> 1234
> John
> Cornor
> Aus
> US
>
> Pleaes give me some suggestion on writing the SQL statement.
>
> Thanks
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:24 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com