28 Aug 2015

Joining Tables in MySQL With example

•Explicit Join in FROM•Joins are implicitly expressed in the WHERE clause
Can Join Only Two Tables at a Time 
         –Use 'JOIN' keyword in the FROM clause
         –Use 'Connecting' Columns in the WHERE clause
datatypes must match or convert correctly
More than one Join operation can be specified in a Single SELECT statement

Join Syntax – FROM Clause:


•SELECT select_list FROM table_1 JOIN table_2   {USING (col_1)   |  ON (col_1 operator col_2) }

Examples:


•SELECT title, name FROM book JOIN publisher USING (pub_id);
•SELECT title, name FROM book JOIN publisher  ON (book.pub_id = publisher.pub_id);
•Qualify the column names with the table names with there is ambiguity

Aliases:


Remember you can use short names as  aliases for the table names

•SELECT title, name FROM book t JOIN publisher p ON (t.pub_id = p.pub_id); 


Share:

0 comments:

Post a Comment