vid
vid

Reputation: 13

insert columns of table2 to table1

I wanted to attach one of the column of table2 to table1 and I used inner join and join none of them worked.

In table1 i have the columns a,b,c

In table2 i have the columns a,d

I wanted the column 'd' to be added with respective columns of 'a'. How can i achieve it

output:

| a | b |c | d |

select t1.a,t2.b,t2.c from table1 t1 JOIN table2 t2 on t1.a=t2.a select t1.a,t2.b,t2.c from table1 t1 RIGHT JOIN table2 t2 on t1.a=t2.a

Upvotes: 1

Views: 55

Answers (1)

dvicemuse
dvicemuse

Reputation: 586

There are 2 problems..

First, your query syntax is incorrect and you arent specifying t2.d In your select statement..

Secondly... you probably want a LEFT JOIN because you dont know if the aggregated data has t2.d Anything missing t2.d would be ignored the way you wrote it.

Not knowing your database... this is what I think will work.

SELECT
`t1`.`a`,
`t1`.`b`,
`t1`.`c`,
`t2`.`d`
FROM
    `table1` AS `t1`
LEFT JOIN
    `table2` AS `t2`
        ON `t2`.`a` = `t1`.`a`

Upvotes: 1

Related Questions