我是SQL的新手,所以请耐心等待.
我正在使用两个没有公共字段的表.我试图基于两个表创建简单的层次结构,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我想编写一个查询,根据表1和表2中第一列中的匹配数值显示表1中的"名称"字段.
因此,如果表1是S10000,则显示A1000 - 测试
那可能吗?
谢谢
您的要求不是很清楚,但听起来您想要这样:
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)
Run Code Online (Sandbox Code Playgroud)
编辑,你也可以使用:
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));
Run Code Online (Sandbox Code Playgroud)