How to get data from two tables in MySQL

Here is example for your queries: how to get data from two tables in MySQL, get data from multiple tables in MySQL, MySQL left join multiple tables, how to get data from two tables in MySQL using php, LEFT JOIN use with example, select data from two tables in MySQL, How to get data from 2 MySQL tables.

Example definition for get data from two tables :

Get 109 user_id user details from two tables, detail get in following sequence user_id, firstname from user table and company_name, company phone from company table.

Example table for get data from two tables :

Here is two example table structure given, both table have one common field i.e., user_id

Table:
user

Fields:
user_id | firstname | lastname | date_create....

Table:
company

Fields:
company_id | user_id | company_name | phone | address | date_create....

MySQL query for get data from two tables :

SELECT u.user_id, u.firstname, c.company_name, c.phone
FROM company c
LEFT JOIN user u ON ( c.user_id = u.user_id )
WHERE c.user_id = 109

MySQL LEFT JOIN query explanation

u.user_id, u.firstname :
We set alias ‘u’ for user table, use ‘u.USER_TABLE_FIELD_NAME’ for access fields
c.company_name, c.phone :
We set alias ‘c’ for user table, use ‘c.COMPANY_TABLE_FIELD_NAME’ for access fields
company c :
Set company table alias ‘c’
user u :
Set user table alias ‘u’
LEFT JOIN user u ON ( c.user_id = u.user_id ) :
Set left join with user table based on common field ‘user_id’
c.user_id = 109 :
Set condition for get only 109 user_id record