lukieleetronic
lukieleetronic

Reputation: 623

MySQL - Left Join, Select all columns left, few columns on the right tables,

I'm trying to join two tables together as shown below, in the " * " section, I'm having trouble doing the following,

Operation: want to select all columns on the left, and just wanna attach few relevant columns from the right table to the left table. Instead of writing all the columns like left_table.column1, left_table.column2 ... so on is there another method which saves the manual coding?

SELECT * FROM nutrients LEFT JOIN measures ON nutrients.name=measures.name

Upvotes: 11

Views: 28877

Answers (3)

Venkata Krishna
Venkata Krishna

Reputation: 1776

SELECT nutrients.*,
       measures.name,
       measures.column2,
       measures.column3
FROM nutrients
LEFT JOIN measures ON nutrients.name=measures.name

You can use this query which selects all from 1st table(left table) and specific columns from right table.

Hope this gives you what you need.

Upvotes: 0

Sadikhasan
Sadikhasan

Reputation: 18600

You can select all column using * like following

SELECT nutrients.*,
       measures.name,
       measures.col2
FROM nutrients
LEFT JOIN measures ON nutrients.name=measures.name

Upvotes: 2

juergen d
juergen d

Reputation: 204854

Yes, add the table name before the * to select all columns of a table

SELECT nutrients.*, measures.colX
FROM nutrients 
LEFT JOIN measures ON nutrients.name=measures.name

Upvotes: 30

Related Questions