简体   繁体   English

在两个可能的列上的SQL LEFT JOIN

[英]SQL LEFT JOIN on two possible columns

We are adding a table to our database schema. 我们正在向我们的数据库模式添加一个表。 It has a relationship to an already existing table, which we added a foreign key for. 它与已经存在的表有关系,我们为其添加了外键。 Mind you, I didn't create this schema nor do I have permission to change much. 提醒您,我没有创建此架构,也没有权限进行大量更改。 The application has been running for a while and they are hesitant to change much. 该应用程序已经运行了一段时间,他们不愿意进行太多更改。

USER_ACTIVITY_T (preexisint table - only relevant columns referred) USER_ACTIVITY_T(preexisint表-仅引用相关列)

  • activity_id (pk) activity_id(pk)
  • username 用户名
  • machineid (fk - recently added) machineid(fk-最近添加)

MACHINE_T (new table) MACHINE_T(新表)

  • machineid (pk - auto increment) machineid(pk-自动递增)
  • machinename (unique) 机器名(唯一)

From the point where I added the machine table, it collects machine data; 从添加机器表的角度来看,它收集机器数据; allowing users to see what machines were involved during the activity. 允许用户查看活动期间涉及的计算机。 This is useful but it only shows data from the point that it was implemented. 这很有用,但仅显示从实现的角度来看的数据。 A lead asked me to attempt to fill preexisting records by referring to the username associated with the machine. 一位负责人要求我尝试通过引用与计算机关联的用户名来填充先前存在的记录。 We understand that this is not 100% accurate but... yeah. 我们知道这不是100%准确,但是...是的。 Our idea was to add username to MACHINE_T and use as a way to populate the machinename in reports retroactively (which assumes that the user has only used one machine and never changed their username). 我们的想法是将用户名添加到MACHINE_T中,并用作追溯地填充报告中的计算机名的方式(假定用户仅使用一台计算机,并且从未更改过用户名)。

So, the new MACHINE_T table would look like: 因此,新的MACHINE_T表将如下所示:

MACHINE_T (new table) MACHINE_T(新表)

  • machineid (pk - auto increment) machineid(pk-自动递增)
  • machinename (unique) 机器名(唯一)
  • username 用户名

Right now, our current SQL is: 现在,我们当前的SQL是:

SELECT * FROM `USER_ACTIVITY_T` LEFT JOIN `MACHINE_T` 
ON MACHINE_T.machineid=USER_ACTIVITY_T.machineid

Anyone have any suggestions on how to join on the username if USER_ACTIVITY_T.machineid is null but has a matching username? 如果USER_ACTIVITY_T.machineid为null但具有匹配的用户名,是否有人对如何加入用户名有任何建议? I'm sorry. 对不起。 This is an odd request that I may spend far too much time over-analyzing. 这是一个奇怪的请求,我可能花太多时间进行过度分析。 Thank you for any help. 感谢您的任何帮助。 I'm almost tempted to just say it can be reasonably done. 我几乎想说它可以合理地做到。

You want to select the joins from a when the joined column is not null and from b when it is null. 当联接列不为空时,您要从a选择联接;如果联接列为空,则要从b选择联接。

You dont want repeat information however so UNION may cause problems on its own. 但是,您不希望重复信息,因此UNION可能会自行导致问题。

Try only selecting the not null entries on the first join and then exclude the null entries from the second join before you union them. 尝试仅在第一个联接上选择非空条目,然后在合并它们之前从第二个联接中排除空条目。

So: 所以:

   SELECT * 
   FROM `USER_ACTIVITY_T` 
   LEFT JOIN `MACHINE_T` 
   ON MACHINE_T.machineid = USER_ACTIVITY_T.machineid

   UNION ALL

   SELECT * 
   FROM `USER_ACTIVITY_T` 
   JOIN `MACHINE_T` 
   ON MACHINE_T.username = USER_ACTIVITY_T.username
   WHERE USER_ACTIVITY_T.machineid IS NULL

This way you are basically using one query for the null entries and one for the not null entries and UNIONing them. 这样,您基本上是对空条目使用一个查询,对非空条目使用一个查询并对其进行UNIONing。

And, I just discovered the UNIION operator which will help me solve this. 而且,我刚刚发现UNIION运算符将帮助我解决这个问题。 However, I am open to other solutions. 但是,我愿意接受其他解决方案。

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

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