Alex
Alex

Reputation: 5685

MySQL Select all columns from one table and some from another table

How do you select all the columns from one table and just some columns from another table using JOIN? In MySQL.

Upvotes: 402

Views: 413582

Answers (4)

Mzila
Mzila

Reputation: 377

select a.* , b.Aa , b.Ab, b.Ac from table1 a left join table2 b on a.id=b.id

this should select all columns from table 1 and only the listed columns from table 2 joined by id.

Upvotes: 35

Himanshu
Himanshu

Reputation: 12675

Using alias for referencing the tables to get the columns from different tables after joining them.

Select tb1.*, tb2.col1, tb2.col2 from table1 tb1 JOIN table2 tb2 on tb1.Id = tb2.Id

Upvotes: 5

Simon
Simon

Reputation: 5247

I need more information really but it will be along the lines of..

SELECT table1.*, table2.col1, table2.col3 FROM table1 JOIN table2 USING(id)

Upvotes: 57

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124758

Just use the table name:

SELECT myTable.*, otherTable.foo, otherTable.bar...

That would select all columns from myTable and columns foo and bar from otherTable.

Upvotes: 669

Related Questions