Kavi suja

Kavi suja

  • NA
  • 298
  • 167.7k

Retrieve data from 3 tables - MySQL

Jul 12 2013 6:47 AM
Hi,
   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?


Answers (6)