1

2番目のIDが最初のIDの最初の4文字と等しい2つのテーブルから選択したい

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

4

1 に答える 1

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 の独自の列である必要があります。

于 2012-07-25T10:37:11.020 に答える