sql - MySQL JOIN two tables with different number of rows by id -
i have 2 tables.
table 1 +----+--------+ | id | column | +----+--------+ | 1 | 23 | +----+--------+ | 2 | 34 | +----+--------+ | 3 | 99 | +----+--------+ table 2 +----+--------+ | id | column | +----+--------+ | 10 | 1 | +----+--------+ | 11 | 1 | +----+--------+ | 21 | 2 | +----+--------+ | 33 | 3 | +----+--------+
i want combine these tables get
table 3 (desired) +--------+--------+ | column | column | +--------+--------+ | 10 | 23 | +--------+--------+ | 11 | 23 | +--------+--------+ | 21 | 34 | +--------+--------+ | 33 | 99 | +--------+--------+
i know how this:
table 3 (actual) +--------+--------+ | column | column | +--------+--------+ | 10 | 23 | +--------+--------+ | 11 | null | +--------+--------+ | 21 | 34 | +--------+--------+ | 33 | 99 | +--------+--------+
but when rows have duplicate id columns, first correct value , rest null.
the query have produce table 3 is
select table2.id, table1.column table2 left join (select .... ) table1 on table1.id = table2.column
how can join tables correct version of table 3?
this should work fine:
select t2.id column1, t1.column column2 table2 t2 left join table1 t1 on t1.id = t2.column;
see in action here:
this give same result want:
| column1 | column2 | --------------------- | 10 | 23 | | 11 | 23 | | 21 | 34 | | 33 | 99 |
Comments
Post a Comment