简体   繁体   English

如何使用MYSQL创建3向左联接?

[英]How do I create a 3-way LEFT JOIN with MYSQL?

I'm attempting to create a 3-way LEFT JOIN using MYSQL and I'm having difficulty accomplishing it so I figured this would be the place to figure it out. 我试图使用MYSQL创建一个3向LEFT JOIN,但是我很难做到这一点,所以我认为这是找出它的地方。

I have a three tables as I'll display below. 我有三个表格,如下所示。 The first contains a list of items that a user has added to their queue to be processed by the game. 第一个包含用户已添加到其队列中以供游戏处理的项目列表。 The other two contain details about each of the items such as their strengths, points to completion, etc. In the queued table, I have two types of items, units and research. 另外两个包含每个项目的详细信息,例如它们的优势,完成点等。在排队的表格中,我有两种类型的项目,即单位和研究。 The unit details are found in table 2 and the research details are found in table 3. 表2中提供了单位详细信息,表3中提供了研究详细信息。

Table 1: The first table (core_queued_units) contains the following fields: id, unit_id, name, location, class(unit or research),sort. 表1:第一个表(core_queued_units)包含以下字段:id,unit_id,名称,位置,类别(单位或研究),排序。

Table 2: The second table (core_available_units) contains the following fields: id, name, description, etc. 表2:第二个表(core_available_units)包含以下字段:id,名称,描述等。

Table 3: The third table (core_available_tech) contains the following fields: id, name, description, etc. 表3:第三个表(core_available_tech)包含以下字段:id,名称,描述等。

FROM core_queued_units
LEFT JOIN core_available_units
ON core_queued_units.unit_id = core_available_units.id
AND core_queued_units.class='Unit'
LEFT JOIN core_available_tech
ON core_queued_units.unit_id = core_available_tech.id
AND core_queued_units.class='Research'
WHERE core_queued_units.location = '1'
AND core_queued_units.user_id ='".$GLOBALS['self']['usrID']."'
ORDER BY core_queued_units.sort ASC

If fields in core_available_units and core_available_tech are equal, you can try this: 如果core_available_unitscore_available_tech中的字段相等,则可以尝试以下操作:

SELECT *
  FROM core_queued_units cq
  LEFT JOIN (
     select cau.*, 'Unit' class from core_available_units cau
      union all
     select cat.*, 'Research' class from core_available_tech cat) c
    ON cq.unit_id = c.unit_id and c.class = cq.class
WHERE cq.location = '1'
  AND cq.user_id ='".$GLOBALS['self']['usrID']."'
ORDER BY cq.sort ASC

If you are trying to join multiple tables, you can do as follows. 如果要联接多个表,可以执行以下操作。 I am taking Table1 as base table. 我将Table1作为基本表。

SELECT columnname1, columnname2......
FROM Table1 
LEFT OUTER JOIN Table2 ON Table1.PK = Table2.FK (here Table2.FK is the Primary key of Table1 which is used as Foreign key in Table2)
LEFT OUTER JOIN Table3 ON Table1.PK = Table3.FK (same condition as above) 
WHERE insert your conditions here 

using this sample, columns from Table2 & Table3 will added at the right most side of Table1. 使用此示例,表2和表3中的列将添加到表1的最右侧。 Please inform if this helps. 请告知这是否有帮助。

Can you try to swap the arguments in the On-clause 您可以尝试交换子句中的参数吗?

core_queued_units.unit_id = core_available_tech.id ? core_queued_units.unit_id = core_available_tech.id吗?

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

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