sql - Combine two tables on same columns -


i have 2 tables showen below:

table 1: name   age  weight(kilo)          tom    16     56          alex   29     89   table 2: name  age   sex          tom   16     m          alex  29     m 

what want get:

table 3: name  age  sex  weight(kilo)          tom   16   m     56          alex  29   m     89 

i have tried union/union , doesn't work. tried use join gives me table duplicate values. idea how this?

assuming name/age values match between 2 tables, join you're looking for.

select t1.name, t1.age, t2.sex, t1.weight table1 t1 join table2 t2 on t1.name = t2.name , t1.age = t2.age 

if there possibility there no match between tables, start 1 larger number of records, left outer join:

for example, assume table1 has every person, table2 may missing some:

select t1.name, t1.age, t2.sex, t1.weight table1 t1 left join table2 t2 on t1.name = t2.name , t1.age = t2.age 

if might have records in either table aren't in other, full outer join work:

select     coalesce(t1.name, t2.name) [name]     ,coalesce(t1.age, t2.age) [age]     ,t2.sex     ,t1.weight table1 t1 full join table2 t2 on t1.name = t2.name , t1.age = t2.age 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo