0

I have two table in my MySQL table:
1) mast_checkup (master)
2) tbl_lab_checkup (child)

Table structure is as follow:
1) mast_checkup:

-- checkid (pk)
-- title
-- description

2) tbl_lab_checkup

-- labcheckupid(pk)
-- labid(fk)
-- mastcheckupid(fk)
-- discount
-- cost

I want to show all the records from master table and all records from child table where labid='1 I tried following query.

SELECT * FROM mast_checkup mc
LEFT JOIN tbl_lab_checkup tlc ON
mc.checkupid=tlc.mastcheckupid WHERE
tlc.ladid=1

Couldn't get exact result.

What I need is all records from patent table and matching from child and if no match found from child it should result all master fields and null child fields

Required Result

----------------------------------------------------------------------------------------
checkid | title   | labcheckupid| labid | discount | cost
----------------------------------------------------------------------------------------
   1    |title 1  |    1    |1      |  5       | 1500

   2    |title 2  |    NULL |NULL   |NULL      |    NULL
----------------------------------------------------------------------------------------

first is a records which is in both master and child table and second one is only in master but not in child

Any solutions for this requirement ?

4

1 回答 1

0

我相信这就是你需要的..

SELECT * FROM mast_checkup mc
LEFT JOIN tbl_lab_checkup tlc ON mc.checkid=tlc.mastcheckupid
AND tlc.labid=1
于 2013-07-04T07:19:33.197 回答