[英]Get all records from master table and matching from child using WHERE mysql query
I have two table in my MySQL table: 我的MySQL表中有两个表:
1) mast_checkup (master)
2) tbl_lab_checkup (child)
Table structure is as follow: 表结构如下:
1) mast_checkup:
-- checkid (pk) -Checkid(pk)
-- title -标题
-- description -说明
2) tbl_lab_checkup
-- labcheckupid(pk) -labcheckupid(pk)
-- labid(fk) -labid(fk)
-- mastcheckupid(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. 我想显示主表中的所有记录以及子表中的所有记录,其中labid ='1我尝试了以下查询。
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 ? 对此要求有解决方案吗?
I believe this is what you need.. 我相信这就是您所需要的。
SELECT * FROM mast_checkup mc
LEFT JOIN tbl_lab_checkup tlc ON mc.checkid=tlc.mastcheckupid
AND tlc.labid=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.