简体   繁体   English

MySQL上的三表交汇

[英]Three Table Intersection on MySQL

Item_List Table: Item_List 表:

在此处输入图像描述

Kaloys Table:卡洛伊表:

在此处输入图像描述

Nanays Table:纳奈斯表:

在此处输入图像描述

Hi, I have three tables as can be seen on the provided images and I'm lost on what to with this task which is to display the Combined List of products that Both Customers (kaloys an Nanays) need, including quantities.嗨,我有三个表格,可以在提供的图像上看到,我不知道如何处理这个任务,即显示两个客户(kaloys 和 Nanays)需要的产品组合列表,包括数量。 Well, I somehow displayed what both customer needs by entering好吧,我通过输入以某种方式显示了两个客户的需求

select Item_List.PID, Item_List.Product, Item_List.supID 
from Item_List where PID in (
   select PID from Kaloys union distinct select PID from Nanays
);

Initial Output:初始 Output:

在此处输入图像描述

but I don't know how to also display the Quantity Column as well.但我也不知道如何显示数量列。 What should I do?我应该怎么办?

You need a FULL OUTER JOIN to do this.您需要FULL OUTER JOIN来执行此操作。 Since MariaDB does not implement those, you'll need to simulate them by combining a LEFT JOIN with a RIGHT JOIN .由于 MariaDB 没有实现这些,因此您需要通过将LEFT JOINRIGHT JOIN组合来模拟它们。 See below:见下文:

select
  l.*, x.kqty, x.nqty
from (
  select k.pid, k.qty as kqty, n.qty as nqty
  from kaloys k left join nanays n on k.pid = n.pid
  union
  select n.pid, k.qty, n.qty
  from kaloys k right join nanays n on k.pid = n.pid
) x
join item_list l on l.pid = x.pid

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

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