简体   繁体   English

内部连接基于一个表中的两列和另一列中的一列的2个表

[英]Inner join 2 tables based on 2 columns in one and one column in the other

在此处输入图片说明

I have two tables I am wanting to inner join. 我有两个要内部连接的表。 The column of one is Ordind000.PROCPLANID . 一个的列是Ordind000.PROCPLANID This has the job, the J000012345 , and suffix, the .00001 both in this one column. 这样做的工作,在J000012345 ,和后缀,在.00001都在此一列。 But in the Job table that I need to join it to, both the job and the suffix are separate columns. 但是在需要将其加入的Job表中,job和后缀都是单独的列。

My guess is I need to use the replace command and write it to a temp table, but I am hoping for a much easier solution. 我的猜测是我需要使用replace命令并将其写入临时表,但我希望有一个更简单的解决方案。 I will also need to strip the decimal at the beginning of the job and suffix numbers in the PROCPLANID columns I think. 我还需要PROCPLANID job开头的小数和我认为的PROCPLANID列中的suffix数字。

There are a lot of ways you could manipulate this, here is one: 您可以通过多种方式来操纵它,这是一种:

SELECT columns
FROM table1 t1
INNER JOIN table2 t2 on 
SUBSTRING(t1.procplanid,2,10) + RIGHT(t1.procplanid,1) = t2.job + t2.suffix

This will compare the job portion of procplanid (without the decimal) + the last digit of procplanid as the suffix , to job + suffix 这会将procplanidjob部分(不带小数点) + procplanid的最后一位作为suffix ,与job + suffix

Highlighted examples in the image will become: 图像中突出显示的示例将变为:

  • J0000784749: Job = J000078474, Suffix = 9 J0000784749:工作= J000078474,后缀= 9
  • J0000760622: Job = J000076062, Suffix = 2 J0000760622:作业= J000076062,后缀= 2

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

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