2016-10-05 7 views
0
を鋳造し、別のテーブルに参加しようと

私の最初のテーブルには、次のとおりです。Postgresのエラーは、ときにエラー

ID | Code | Description 
1 | IT  | Informatics 
2 | GAM | Gamer 
3 | PROG | Programmer 

私の2番目の表は、次のようになります。

ID | Order | Catcod 
1 | 8080 | {IT,GAM} 
2 | 7051 | 
3 | 5601 | PROG 

お知らせその最初の表の「コード」欄Varchar列であり、2番目の表でCatcod列はVarchar []列です。

私が実行しようとするSQLは、次のとおりです。エラーイムなっている

SELECT * 
FROM table2 RIGHT OUTER JOIN 
    table1 
    ON table2.Catcod = table1.Code 

ERROR: operator does not exist: character varying[] = character varying 
LINE 4: RIGHT OUTER JOIN gc1pbcat on gc1vrkordhdr.catcod = gc1pbcat... 
                 ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
********** Error ********** 

ERROR: operator does not exist: character varying[] = character varying 
SQL state: 42883 
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
Character: 216 

誰もが私はちょうどvarchar型に[] varchar型に変換することができますどのような方法を知っていますか多分アレイを分割する?

私は表2のVarchar []にCatcodeの説明を表示したいからです。

UPDATE

それとも誰かが私はVARCHARに[] varchar型に変換することができますどのように私の質問にawnserがありますか?

+0

どの条件テストをすべきか?配列の要素のいずれかが他の側と等しい場合は? –

+0

私はtable2のCatcod []と等しいコードでtable1の説明を取得しようとしています。 – Antiwoutertje

答えて

2

使用LEFT JOINany()

SELECT * 
    FROM table2 
    LEFT JOIN table1 on table1.code = any(table2.catcod); 

id | order | catcod | id | code | description 
----+-------+----------+----+------+------------- 
    1 | 8080 | {IT,GAM} | 1 | IT | InfOrmatics 
    1 | 8080 | {IT,GAM} | 2 | GAM | GaMer 
    2 | 7051 |   | |  | 
    3 | 5601 | {PROG} | 3 | PROG | PRogrammer 
(4 rows)  

ご注文の1行を取得したい場合は、string_agg()を使用し、例えば:

SELECT table2.id, table2.order, string_agg(description, ', ') description 
    FROM table2 
    LEFT JOIN table1 on table1.code = any(table2.catcod) 
GROUP BY 1, 2; 

id | order | description  
----+-------+-------------------- 
    1 | 8080 | InfOrmatics, GaMer 
    2 | 7051 | 
    3 | 5601 | PRogrammer 
(3 rows) 
関連する問題