我有这个案例,我可以这样做吗?
表A:
id | name | trx
1 blue 2
2 yellow 1
3 red 3
表B:
id | key | value
1 a K1
2 b K2
3 c K3
那么在表B上,希望将字母表转换为表A 上的trx
a = 1
b = 2
c = 3
那么表B上的字段"key"是否可以作为外键与字段为"trx"的表A连接?
您可以使用ASCII函数将单个字符转换为它们的相对数值。
with alpha (letter) as
( values ('a'), ('b'), ('c'), ('z') )
select letter || ' = ' || ascii(letter) - ascii('a') + 1
from alpha;
但请注意,这仅适用于单个小写字母。相应的大写版本是"ascii(字母(-ascii('A'(+1">
由于您确实没有指定任何预期结果,我只能在给定的表/数据上推测以下连接:
with Table_A (id,name,trx) as
( values (1,'blue',2)
, (2,'yellow',1)
, (3,'red ',3)
)
, Table_B (id,key,value) as
( values (1,'a','K1')
, (2,'b','K2')
)
select a.name,b.value
from table_a a
join table_b b
on (ascii(b.key) - ascii('a') + 1) = a.id;