我想从2表中选择第二个id等于第一个id
的前4个字符SELECT a.*, b.*, substring(a.my_id from 1 for 4)::integer as number
FROM table1 as a
INNER Join table2 as b ON(b.id_2=number) where my_id = 101
^
It produces an error here |
错误:列“数字”不存在
SQL状态:42703
性格:189
答案 0 :(得分:3)
你不能使用这样的别名,你需要一个派生表:
select *
from (
SELECT t1.*,
substring(t1.my_id::text from 1 for 4)::integer as number
FROM table1 t1
) as a
inner join table2 as b ON (b.id_2 = a.number)
where my_id = 101
存储一个用作外键的数字作为varchar列中的一部分是一个非常非常丑陋的设计。该数字应该是table1中的一个列。