Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only some columns from a table on a LEFT JOIN

Tags:

join

mysql

Is it possible to select only some columns from a table on a LEFT JOIN?

like image 279
Psyche Avatar asked Aug 25 '09 17:08

Psyche


People also ask

How do I combine only certain columns?

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

How do I SELECT only certain columns in SQL?

To select columns, choose one of the following options: Type SELECT , followed by the names of the columns in the order that you want them to appear on the report. Use commas to separate the column names.

How do I SELECT only few columns?

Either specify * , and get all columns, or specify each one you want, from col5 to col1000. (1000 columns indicates a poorly designed database.) If you could tag which particular DBMS are you using, you might find a solution involving dynamic SQL with a metadata query that builds your SQL for you.


2 Answers

Of course. Just list the columns you want to select as you would in any query:

SELECT table1.column1, table1.column2, table2.column3 FROM table1 LEFT JOIN table2 ON (...) 

Note that I've included the table1. or table2. prefix on all columns to be sure there aren't any ambiguities where fields with the same name exist in both tables.

like image 100
VoteyDisciple Avatar answered Sep 22 '22 04:09

VoteyDisciple


Add a * to just that table in your select statement, separate from other columns with a comma:

SELECT table1.*, table2.col2, table2.col3 FROM table1 LEFT JOIN table2 ON... 

Source: https://stackoverflow.com/a/3492919/3417198

like image 38
jlansey Avatar answered Sep 21 '22 04:09

jlansey