你好,有一个v_message
视图,看起来像这样
SELECT
message.id AS "id",
body AS "body",
message.created_at AS "createdAt",
from_phone_number AS "fromPhoneNumber",
to_phone_number AS "toPhoneNumber",
is_read AS "isRead",
json_build_object(
'id', agent.id,
'firstName', agent.first_name,
'lastName', agent.last_name,
'avatarLink', agent.avatar_link
) AS "createdByAgent",
json_build_object(
'id', client.id,
'firstName', client.first_name,
'displayName', client.display_name
) AS "createdByClient"
FROM message
LEFT JOIN agent
ON message.created_by_agent_id = agent.id
LEFt JOIN client
ON message.created_by_client_id = client.id
我现在想应用一个WHERE
查询,如下所示
SELECT * FROM v_message WHERE v_message."createdByClient".id = 1
但是,我遇到了以下错误:
ERROR: missing FROM-clause entry for table "createdByClient"
2条答案
按热度按时间pw9qyyiw1#
必须使用运算符
->>
以text/varchar的形式获取元素id
。由于返回的结果是文本,我们必须使用
::integer
将其转换为整数,然后将其与integer
进行比较:或者将
1
转换为varchar:gcxthw6b2#
JSON对象的元素不能用点表示法访问。正确的语法是
(v_message."createdByClient" -> 'id')::integer = 1
,而不是v_message."createdByClient".id = 1
。