简体   繁体   English

使用WHERE mysql查询从主表中获取所有记录,并从子表中进行匹配

[英]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.

相关问题 如何使用php从mysql数据库中获取所有匹配的记录? - how to get all matching records from mysql database using php? 使用codeigniter从两个表中获取记录,其中所有匹配的记录都来自第一个表 - Get Record from two table using codeigniter where all matching record from first table 如何使用存储在数组中的php从MySql数据库中获取所有匹配记录的尊重? - How to get all matching records respectievelijk from MySql database using php which is stored in an array? Codeigniter 查询未返回左表中的所有记录。 只返回匹配的记录 - Codeigniter query is not returning all records from the left table. Only returns the matching records 如何从Codeigniter中的mysql表获取所有记录 - how to get all the records from mysql table in codeigniter MySQL查询从一系列记录中获取记录 - Mysql query to get records from a range of records MySQL在一次查询中从表中获取相关记录和不相关记录 - MySQL get Related and Unrelated Records from Table in One Query 如何从PHP Web服务中的MySQL表获取所有记录 - how to get all the records from MySQL table in php web services 在主表中列出所有mysql记录,在相关子表中列出最后一条记录 - List all mysql records in a master table and last record in a related child table 使用 where in 条件从表中获取最新的 N 条记录 - Get Latest N numbers of records from table using where in condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM