Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - specific columns on join?

Tags:

sql

mysql

When making a join (inner, left outer, right outer or whatever), how can I specify which columns on the table to join into the original table?

Consider the following example:

SELECT FirstName FROM User LEFT OUTER JOIN Provider ON User.ProviderID = Provider.ID

This would select FirstName from user, but select everything from Provider. How can I specify which parts of Provider should be included in the resultset?

like image 383
Mathias Lykkegaard Lorenzen Avatar asked Jul 26 '11 06:07

Mathias Lykkegaard Lorenzen


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.

How do I select a column in inner join?

The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables. SELECT * FROM table1 JOIN table2 ON table1. column_name = table2.

How do I select a specific column from a table in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.

How do I join two tables in different column names in SQL?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.


1 Answers

This will only include User.FirstName and Provider.ProviderID in the final resultset:

SELECT User.FirstName, Provider.ProviderID FROM User LEFT OUTER JOIN Provider ON User.ProviderID = Provider.ID
like image 128
Tudor Constantin Avatar answered Oct 21 '22 23:10

Tudor Constantin