Suresh kumar
Suresh kumar

Reputation: 2092

MySQL Query to retrieve data from two tables even no data found in second table

I want to fetch a data from two tables even if the second table has no data. I need empty values for no data.

For e.g. table 1 has the fields

table 2 has

Some time table 2 doesn't have the value for a particular user. For that I need the null values.

Here I give sample input and expected output

table1
id name password
1  nam1  pass1
2  nam2  pass2

table 2
id f1 f2 f3
1   1  2  3

sample output
id name password f1 f2 f3
1  nam1  pass1   1  2  3
2  nam2  pass2   null null null

I need a query to fetch a data.

Upvotes: 2

Views: 4561

Answers (2)

JDC
JDC

Reputation: 1785

http://www.w3schools.com/sql/sql_join_left.asp

You want to look to left join and right join

Upvotes: 1

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

You need SELECT with LEFT JOIN

Sample query will be like:

SELECT 
    table1.id,table1.name,table1.password,table2.f1,
    table2.f2,table2.f3 
FROM table1 
LEFT JOIN table2 
ON table1.id=table2.id

Upvotes: 7

Related Questions