Jump to content
Sign in to follow this  
fanton123

Вывод минимальной цены из всех товаров в категории исключая ноль

Recommended Posts

Добрый день. Opencart 2.1.0.2.1. Стоит задача вывести на всех категориях минимальную и максимальную цену товара. С максимальной ценой проблем нет, всё работает. Не получается вывести минимальную цену исключая нулевую цену товара.

Запрос на максимальную цену товара:

Спойлер

SELECT p.price, ps.price AS price_special FROM ".DB_PREFIX."product p LEFT JOIN ".DB_PREFIX."product_to_category AS ptc ON ptc.product_id = p.product_id LEFT JOIN ".DB_PREFIX."product_special ps ON (p.product_id=ps.product_id) WHERE ptc.category_id = ".(int)$category_id." OR ptc.category_id IN (SELECT path_id FROM ".DB_PREFIX."category_path WHERE category_id = ".(int)$category_id." AND level = 0) AND p.price > 0 AND p.quantity > 1 AND p.date_available <= NOW() ORDER BY p.price DESC LIMIT 0,1

 

А вот запрос на минимальную цену товара упорно выводит 0(нулевую цену товара):

Спойлер

SELECT p.price, ps.price AS price_special FROM ".DB_PREFIX."product p LEFT JOIN ".DB_PREFIX."product_to_category AS ptc ON ptc.product_id = p.product_id LEFT JOIN ".DB_PREFIX."product_special ps ON (p.product_id=ps.product_id) WHERE ptc.category_id = ".(int)$category_id." OR ptc.category_id IN (SELECT path_id FROM ".DB_PREFIX."category_path WHERE category_id = ".(int)$category_id." AND level = 0) AND p.price > 0 AND p.quantity > 1 AND p.date_available <= NOW() ORDER BY p.price ASC LIMIT 0,1

 

Share this post


Link to post
Share on other sites
SELECT 
	MIN(IFNULL(ps.price, p.price)) AS min_price, 
	MAX(IFNULL(ps.price, p.price)) AS max_price
FROM ".DB_PREFIX."product AS p 
LEFT JOIN ".DB_PREFIX."product_to_category AS ptc USING(product_id) 
LEFT JOIN ".DB_PREFIX."product_special AS ps USING(product_id) 
WHERE IFNULL(ps.price, p.price) > 0
AND (ptc.category_id = ".(int)$category_id." 
	OR ptc.category_id IN (SELECT path_id FROM ".DB_PREFIX."category_path WHERE category_id = ".(int)$category_id." AND level = 0)) 
AND p.quantity > 1 
AND p.date_available <= NOW() 

 

Если для всех категорий с учетом подкатегорий - то одним запросом вот так

SELECT 
	path_id AS category_id,
	MIN(IFNULL(ps.price, p.price)) AS min_price, 
	MAX(IFNULL(ps.price, p.price)) AS max_price
FROM ".DB_PREFIX."product AS p 
LEFT JOIN ".DB_PREFIX."product_to_category AS ptc USING(product_id) 
LEFT JOIN ".DB_PREFIX."oc_category_path USING(category_id)
LEFT JOIN ".DB_PREFIX."product_special AS ps USING(product_id) 
WHERE IFNULL(ps.price, p.price) > 0
AND path_id IS NOT NULL
AND p.quantity > 1 
AND p.date_available <= NOW() 
GROUP BY path_id

 

  • +1 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.