我目前有一个看起来像这样的PostgreSQL表,数据列是JSON,我试图根据JSON数据中可能存在或不存在的键来查询表。
| A | B | C | data |
----------------------------------------------------
| 1 | 2 | 3 | {} |
| 2 | 3 | 3 | {"name": "jack", "message": "123"} |
| 3 | 4 | 3 | {"name": "jill", "voice": "456"} |
| 4 | 2 | 3 | {"name": "bill", "email" "789"} |
目前我有
SELECT *
FROM (
SELECT
a,
b,
c,
coalesce(
CASE
WHEN (data ->> 'message') IS NULL
THEN NULL
ELSE (data ->> 'message')
END,
'') AS message,
coalesce(
CASE
WHEN (data ->> 'voice') IS NULL
THEN NULL
ELSE (data ->> 'voice')
END,
'') AS voice,
coalesce(
CASE
WHEN (data ->> 'email') IS NULL
THEN NULL
ELSE (data ->> 'email')
END,
'') AS email
FROM mytable) AS t
WHERE (t.message = "789" OR
t.voice = "789" OR
t.email = "789");
我目前收到错误,ERROR:列“789”不存在
我知道可能有更有效的方法。任何帮助非常感谢。
答案 0 :(得分:1)
使用单引号t.message = '789'