我遇到了一个非常复杂的SQL查询问题:
SELECT u."id"
FROM "users" u
JOIN "users_rosters_schedules" urs ON u."id" = urs."user_id"
JOIN "users_postcoderegion_groups" upg ON urs."user_postcoderegion_group_id" = upg."id"
JOIN "postcode_regions" pr ON pr."id" = "users_postcoderegion_groups_postcoderegions"."postcode_region_id"
WHERE u."role" = 'contractor'
AND u."contractor_status" = 'active'
AND urs."day" = '2023-12-01'
AND urs."active" = true
AND (
EXISTS (
SELECT *
FROM jsonb_array_elements(to_jsonb(urs.capacity -> 'products')) AS i(item)
WHERE (item ->> 'product_category_id')::int IN (1, 2)
AND (item ->> 'qty')::int > (
SELECT COUNT(o.id)
FROM orders o
JOIN orders_products_categories opc ON o.id = opc.order_id
WHERE o.contractor_user_id = urs."user_id"
AND o.status != 'cancelled'
AND o.scheduled_pickup_date = urs."day"
AND opc.product_category_id = CAST(i.item ->> 'product_category_id' AS INTEGER)
)
)
AND EXISTS (
SELECT *
FROM "users_postcoderegion_groups" upg_nested
WHERE upg_nested."id" = urs."user_postcoderegion_group_id"
AND EXISTS (
SELECT *
FROM "postcode_regions" pr_nested
JOIN "users_postcoderegion_groups_postcoderegions" upgrp ON pr_nested."id" = upgrp."postcode_region_id"
WHERE pr_nested."postcode_region_id" = 8123
AND upgrp."user_postcode_region_group_id" = upg_nested."id"
)
)
)
AND u."id" = urs."user_id";
字符串
当它执行时,它给我一个错误:
错误代码:表“users_postcoderegion_groups_postcoderegions”的FROM子句条目缺失行5:JOIN“postcode_regions”pr ON pr.“id”=“users_postcoderegio.
有谁能告诉我怎么解决这个问题吗?
1条答案
按热度按时间arknldoa1#
您正在联接到
users_postcoderegion_groups_postcoderegions
,但它并不作为联接表存在,它只存在于子查询中,无法在您在JOIN "postcode_regions" pr ON pr."id" = "users_postcoderegion_groups_postcoderegions"."postcode_region_id"
中使用它的地方进行访问