Gowthamr.cs
Gowthamr.cs

Reputation: 141

Mysql query (join)

I have two tables tbl_user and tbl_lastchangepassword as shown below

Table tbl_user

id|  name 
---------
1 | user1 
2 | user2 
3 | user3

Table tbl_lastchangepassword

id | loginid | date_password
----------------------------
1  |    1    |  2014-03-29

Relation of Above table (tbl_user.id = tbl_lastchangepassword.loginid)

How do I get output as

id | name| date_password
---------------------
1  |user1 | 2014-03-29 
2  |user2 | null
3  |user3 | null

Thank you.

Upvotes: 1

Views: 41

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

Try this

SELECT U.id,U.name,P.date_password FROM tbl_user U 
LEFT JOIN tbl_lastchangepassword P ON U.id= P.loginid

Upvotes: 2

bgs
bgs

Reputation: 3213

Try this

Select a.id, a.name, b.date_password from tbl_user As a
left join tbl_lastchangepassword As b on a.id = b.loginid

OR

Select a.id, a.name, 
( select b.date_password from tbl_lastchangepassword As b where b.loginid = a.Id) As date_password
 from tbl_user As a

Upvotes: 1

Related Questions