I have three tables.the table "commoninfo" contains "name,DOB,Education,Speciality" fields.Then,the table "contactinfo" contains "mobileno,commonid" fields.Here 'commonid' is the foreign key from "commoninfo" table.Then,the table "department" contains "department,commonid" fields.Here 'commomid' is the foreign key from"commoninfo" table.All data's in 'commominfo' table also available in 'department' table.But all the data in 'commominfo' table not available in 'contactinfo' table.Some of the data are missing in 'contactinfo' table.Now I want to get the records from these 3 tables based on 'commonid' field.i use the following:
CREATE DEFINER=`root`@`%` PROCEDURE `SP_EditDoct`(in DID int)
BEGIN
SELECT DoctInfo.firstname,DoctInfo.middleinitial,DoctInfo.lastname,
Date_Format(DoctInfo.dateofbirth,'%m%d%y'),DoctInfo.education,DoctInfo.Speciality,
DoctInfo.gender,ContInfo.value as MobileNo,Dept.Deptid
FROM
commonpersoninfo DoctInfo,contactinfo ContInfo,departmentview Dept
LEFT JOIN contactinfo ON DoctInfo.pkey=ContInfo.whosInfo_pkey
WHERE DoctInfo.pkey = DID
AND DoctInfo.pkey=Dept.CommonPersonInfo_Pkey
AND DoctInfo.pkey=ContInfo.whosInfo_pkey;
END
But the problem here is,it retrieves which data is available in 3 tables.I want to generate data,if it available in 'commominfo' table only. Can i use left join her?how i use it here?Can anyone help me to do this?

Iftikar HussainPosted Jul 12, 2013, 9:01 AM
try this.
Regards,
Iftikar
Kavi sujaPosted Jul 12, 2013, 9:08 AM
Kavi sujaPosted Jul 12, 2013, 8:45 AM
Sanjeeb LenkaPosted Jul 12, 2013, 8:21 AM
SELECT
DoctInfo.firstname,DoctInfo.middleinitial,DoctInfo.lastname,
Date_Format(DoctInfo.dateofbirth,'%m%d%y'),DoctInfo.education,DoctInfo.Speciality,
DoctInfo.gender,ContInfo.value as MobileNo,Dept.Deptid
FROM
contactinfo ContInfo
left JOIN commonpersoninfo DoctInfo ON DoctInfo.pkey=ContInfo.whosInfo_pkey
left join departmentview Dept on Dept.pkey=ContInfo.whosInfo_pkey
WHERE
DoctInfo.pkey = DID
AND DoctInfo.pkey=Dept.CommonPersonInfo_Pkey
AND DoctInfo.pkey=ContInfo.whosInfo_pkey;
Kavi sujaPosted Jul 12, 2013, 8:08 AM
Thank you for your reply.Actually this returns the same result as previous one.For example, 'commoninfo' contains data for PID=55,'department' contains data for PID=55,But 'contactinfo' doesn't contain data for PID=55.So,when I give 55 for PID,it'll return no records.But I want the data available in 'commoninfo' and 'department' tables.How I return this?
Ravi ShekharPosted Jul 12, 2013, 7:32 AM
Try This