[英]Joining two tables on based on second number value from first table
I am new to SQL, so please bear with me. 我是SQL的新手,所以请多多包涵。
I am working with two tables that do not have common fields. 我正在使用两个没有公用字段的表。 I am trying to create simple hierarchy based on the two tables that look like this:
我试图基于如下所示的两个表创建简单的层次结构:
Table 1.
Cul 1 Cul 2
==============
S10000 Name
S20000 Name 1
S30000 Name 2
S40000 Name 3
Table 2
Cul 1 Cul 2
=====================
A10000 Test
A10001 Test 123
A20000 Test 1
A20001 Test 999
A30000 Test 2
A30002 Test 5555
A40000 Test 3
A40006 Test 84384848
I would like to write a query that will display 'Name' field from Table 1 based on matching numeric values in the first columns from table 1 & 2. 我想编写一个查询,该查询将基于表1和2的第一列中的匹配数值显示表1中的“名称”字段。
So if Table 1 is S10000, display A1000 – Test 因此,如果表1是S10000,则显示A1000 –测试
Is that possible? 那可能吗?
Thank you 谢谢
Your requirements are not exactly clear but it sounds like you want this: 您的要求并不十分清楚,但听起来像是您想要这样:
select t1.col1 t1Name,
t1.col2 t1Value,
t2.col1 t2Name,
t2.col2 t2Value
from table1 t1
inner join table2 t2
on substring(t1.col1, 2, 1) = substring(t2.col1, 2, 1)
edit, you can also use: 编辑,您也可以使用:
select t1.col1 t1Name,
t1.col2 t1Value,
t2.col1 t2Name,
t2.col2 t2Value
from table1 t1
inner join table2 t2
on substring(t1.col1, 2, len(t1.col1))
= substring(t2.col1, 2, len(t2.col1));
SELECT
t2.*
FROM Table1 t1
INNER JOIN Table2 t2 on SUBSTRING(t1.Cul1,2,LEN(t1.Cul1)-1)=SUBSTRING(t2.Cul1,2,LEN(t2.Cul1)-1)
If you are very sure that the first digit will be the digit and followed by the numeric then the below should work 如果您非常确定第一个数字是该数字,然后是数字,那么下面的内容应该可以工作
SELECT A.cul1,B.cul2
FROM dbo.Table1 A, dbo.Table2 B
WHERE substring(A.cul1,2,len(A.cul1))= substring(B.cul1,2,len(B.cul1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.