简体   繁体   English

MySql 查询从左表获取所有行,从右表获取公共行

[英]MySql query Get all rows from left table and common from right table

Table 1 - customer_kitchen_service_plans and data https://prnt.sc/00_ip7uWiQuq表 1 - customer_kitchen_service_plans和数据https://prnt.sc/00_ip7uWiQuq

Table 2 - kitchen_service_plans_linking and data https://prnt.sc/e_GW64THTCFK表 2 - kitchen_service_plans_linking和数据https://prnt.sc/e_GW64THTCFK

Above two are the tables and i want to join in such a way that it should return All the rows from Table 1 and Common rows from Table 2以上两个是表,我想以这样的方式加入,它应该返回表 1 中的所有行和表 2 中的常见行

Join using column kitchen_service_plan_id from Table 1 and kitchen_service_plan_parent_id from Table 2使用表 1 中的kitchen_service_plan_id列和表 2 中的kitchen_service_plan_parent_id列加入

Current query is as below当前查询如下

select * from `customer_kitchen_service_plans` as `cksp` 
left join `kitchen_service_plans_linking` as `kspl` on 
  `kspl`.`kitchen_service_plan_parent_id` = 
  `cksp`.`kitchen_service_plan_id` 
where `cksp`.`status` = 'ACTIVE' and `cksp`.`customer_id` = 2

See if that's help看看有没有帮助

SELECT * FROM customer_kitchen_service_plans 
LEFT JOIN kitchen_service_plans_linking ON 
          customer_kitchen_service_plans.kitchen_service_plan_id= 
          kitchen_service_plans_linking.kitchen_service_plan_parent_id;

You want a left outer join which returns all rows from the first table and matching rows from the right.您需要一个左外连接,它返回第一个表中的所有行并从右侧返回匹配行。

select * from `customer_kitchen_service_plans` as cksp 
left outer join `kitchen_service_plans_linking` as kspl on 
  kspl.`kitchen_service_plan_parent_id` = 
  cksp.`kitchen_service_plan_id` 
where cksp.`status` = 'ACTIVE' and cksp.`customer_id` = 2

Here's a discussion on Left Outer Join in MySQL这是关于MySQL 中的左外连接的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM