我需要顶级查询返回返回多个字段的子查询数组。由于以下错误,简单select array(select ...);
无效:
子查询必须只返回一列
查询的一部分:
(SELECT array(SELECT
logs_log.message -> 'slot_date',
logs_log.message -> 'start_time',
logs_log.message -> 'end_time',
logs_log.message -> 'source',
logs_log.message -> 'phone_number'
FROM
logs_log
WHERE
logs_log.message ->> 'phone_number' = leads_lead.phone_number))
AS previous_appointments
任何想法如何实现上述结果?
logs_log表:
id, object_id, action_time, content_type_id, user_id, message
28138, 0d36a20d-a251-41c3-ba7e-2d270004f4b1, 2017-01-27 06:48:06.550265+00, 18, 1, {"source": "driver_email", "end_time": "13:30:00", "slot_date": "0333-03-23", "start_time": "13:00:00", "phone_number": "1231231231"}
28137, 0d074daa-0c77-4f96-b512-248cdfb9263b, 2017-01-24 21:31:16.140453+00, 18, 1, {"source": "driver_email", "end_time": "13:30:00", "slot_date": "3333-03-23", "start_time": "13:00:00", "phone_number": "9111111992"}
leads_lead表:
id, first_name, second_name, phone_number, city_id, source_id, external_id, attributes, created_at, ip_address, partner_id, tags
ab99dbba-d339-407a-8e2d-c0ffd676213b,Денис,Антонов,7123123123,b3b62c53-6815-4898-b3b6-23a1196c1986,7ee38a21-32d0-4362-8519-0bc6e6ce37b8,20391438-4d19-48b3-aae0-8d1ab0d7ef93,"""{\""uuid\"":\""20392223-4d19-48b3-aae0-8d1ab0d7ef93\""}""",2016-11-07 17:30:00.583425+00,,,
答案 0 :(得分:1)
将您的子选择更改为
(SELECT array_agg(
array[
logs_log.message -> 'slot_date',
logs_log.message -> 'start_time',
logs_log.message -> 'end_time',
logs_log.message -> 'source',
logs_log.message -> 'phone_number'
]
)
FROM logs_log
WHERE logs_log.message ->> 'phone_number' = leads_lead.phone_number
) AS previous_appointments