2017-03-13 11 views
0

私は、「データ列のJSON内のfoo_idがバーテーブルにないすべてのレコードを取得する」というクエリを作成しようとしています。私が試した:Postgres: "NOT IN"とJSON " - >>"を結合するには?

select * from json_table where (data->>'foo_id') not in (
    select bar.id from bar where bar.id is not null 
); 

はしかし、私はエラーを取得:?

ERROR: operator does not exist: text = integer 
+1

ところで、おそらく、使い方は 'EXISTS'は、より効率的になり、NOT:'存在しないところjson_table SELECT * FROM(bar.idがnullでなく、(データどこバーから選択* - >> 'foo_id') :: int = bar.id); ''(bar.idがnullでないという条件もあります。) – Abelisto

+0

おかげで、ありがとう。 – machineghost

答えて

2

を試してみてください。

ERROR: operator does not exist: text = integer 

明示的な型キャスト!

select * from json_table where (data->>'foo_id')::integer not in (
    select bar.id from bar where bar.id is not null 
); 
1

は...あなたの編集に関連して

select * from json_table where (data->>'foo_id') not in (
    select bar.id::text from bar where bar.id is not null 
); 
+0

それはそれでした。私はあなたの答えを提出した直後にこれを実際に見つけ出しました。私は他の方法( '' data - >> 'media_id' :: integer'')だけでした。将来の参照のために、どちらかが機能します(ポイントは型の並び替えを行うことです)。ありがとう! – machineghost

関連する問題