Я пытаюсь создать один запрос для получения текущей цены и специальной цены распродажи, если распродажа проводится; Когда распродажи нет, я хочу, чтобы store_picture_monthly_price_special.price AS special_price возвращал значение null.
Перед добавлением второго условия WHERE запрос выполняется, как я и ожидал: store_picture_monthly_price_special.price возвращает null, поскольку в настоящее время распродаж нет.
store_picture_monthly_reference | tenure | name | regular_price | special_price
3 | 12 | 12 Months | 299.99 | {Null}
2 | 3 | 3 Months | 79.99 | {Null}
1 | 1 | 1 Month | 29.99 | {Null}
pgSQL обрабатывает второе условие WHERE как "все или ничего". Если нет продаж, нет и результатов.
Можно ли настроить этот запрос, чтобы я каждый раз получал регулярные цены и специальную цену продажи либо в виде долларового значения, когда выполняется специальное предложение, либо возвращает null. Требуется ли выполнить то, что я пытаюсь сделать? подзапрос?
Это запрос, как он у меня сейчас есть:
SELECT store_picture_monthly.reference AS store_picture_monthly_reference , store_picture_monthly.tenure , store_picture_monthly.name , store_picture_monthly_price_regular.price AS regular_price , store_picture_monthly_price_special.price AS special_price
FROM ( store_picture_monthly INNER JOIN store_picture_monthly_price_regular ON store_picture_monthly_price_regular.store_picture_monthly_reference = store_picture_monthly.reference )
FULL OUTER JOIN store_picture_monthly_price_special ON store_picture_monthly.reference = store_picture_monthly_price_special.store_picture_monthly_reference
WHERE
( store_picture_monthly_price_regular.effective_date < NOW() )
AND
( NOW() BETWEEN store_picture_monthly_price_special.begin_date AND store_picture_monthly_price_special.end_date )
GROUP BY store_picture_monthly.reference , store_picture_monthly_price_regular.price , store_picture_monthly_price_regular.effective_date , store_picture_monthly_price_special.price
ORDER BY store_picture_monthly_price_regular.effective_date DESC
Таблица "store_picture_monthly"
reference bigint,
name text,
description text,
tenure bigint,
available_date timestamp with time zone,
available_membership_reference bigint
Таблица store_picture_monthly_price_regular
reference bigint ,
store_picture_monthly_reference bigint,
effective_date timestamp with time zone,
price numeric(10,2),
membership_reference bigint
Таблица store_picture_monthly_price_special
reference bigint,
store_picture_monthly_reference bigint,
begin_date timestamp with time zone,
end_date timestamp with time zone,
price numeric(10,2),
created_date timestamp with time zone DEFAULT now(),
membership_reference bigint
SELECT COALESCE(sp.price, no.price) FROM normalprices no LEFT JOIN specialprices sp ON no.prodid=sp.prodid AND NOW() BETWEEN sp.promostart AND sp.promoend
- это объединяет две таблицы по идентификатору продукта и выводит специальные цены только в том случае, если период акции действует, затем объединение дает получить специальную цену или если ноль получить нормальную цену - person Caius Jard   schedule 16.03.2019