Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select proper columns from JOIN statement

I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.

SELECT * FROM table1 AS T1 INNER JOIN table2 AS T2 ON T1.ID = T2.ID

I want to select all columns from table1 and only 1 column from table2. Is it possible to do that without enumerating all columns from table1 ?

like image 233
Alexander Stalt Avatar asked Apr 05 '10 08:04

Alexander Stalt


People also ask

How do I join only some columns in SQL?

Just list the columns you want to select as you would in any query: SELECT table1. column1, table1. column2, table2.

Can we use select statement in join?

So, an SQL Join clause in a Select statement combines columns from one or more tables in a relational database and returns a set of data. The From is also an essential part of the Select statement and this is where it's specified which table we're pulling data from.

Which join query will give correct output?

A Natural Join is also a Join operation that is used to give you an output based on the columns in both the tables between which, this join operation must be implemented.

How do I know which tables join in SQL?

Different Types of SQL JOINs(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.


1 Answers

Yes, you can do the following:

SELECT t1.*, t2.my_col FROM table1 AS T1 INNER JOIN table2 AS T2 ON T1.ID = T2.ID
like image 162
Daniel Vassallo Avatar answered Oct 05 '22 02:10

Daniel Vassallo