0

For those familiar with Prestashop, I am trying to add an extra sort order option in the category view. More specifically I want to add extra sort order for a selection of features.

This is the main part of the prestashop query to get the products: (the last column in the SELECT part as well as the last JOIN added by me)

$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`,
                    pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, MAX(image_shop.`id_image`) id_image,
                    il.`legend`, m.`name` AS manufacturer_name, cl.`name` AS category_default,
                    DATEDIFF(product_shop.`date_add`, DATE_SUB(NOW(),
                    INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).'
                        DAY)) > 0 AS new, product_shop.price AS orderprice, fp.`id_feature_value`
                FROM `'._DB_PREFIX_.'category_product` cp
                LEFT JOIN `'._DB_PREFIX_.'product` p
                    ON p.`id_product` = cp.`id_product`
                '.Shop::addSqlAssociation('product', 'p').'
                LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
                ON (p.`id_product` = pa.`id_product`)
                '.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
                '.Product::sqlStock('p', 'product_attribute_shop', false, $context->shop).'
                LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
                    ON (product_shop.`id_category_default` = cl.`id_category`
                    AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
                LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
                    ON (p.`id_product` = pl.`id_product`
                    AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
                LEFT JOIN `'._DB_PREFIX_.'image` i
                    ON (i.`id_product` = p.`id_product`)'.
                Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
                LEFT JOIN `'._DB_PREFIX_.'image_lang` il
                    ON (image_shop.`id_image` = il.`id_image`
                    AND il.`id_lang` = '.(int)$id_lang.')
                LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
                    ON m.`id_manufacturer` = p.`id_manufacturer`
                LEFT JOIN `'._DB_PREFIX_.'ps_feature_product` fp
                    ON p.`id_product` = fp.`id_product`
                WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
                    AND cp.`id_category` = '.(int)$this->id
                    .($active ? ' AND product_shop.`active` = 1' : '')
                    .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
                    .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
                    .' GROUP BY product_shop.id_product';

The table ps_feature_product looks like this:

CREATE TABLE IF NOT EXISTS `ps_feature_product` (
  `id_feature` int(10) unsigned NOT NULL,
  `id_product` int(10) unsigned NOT NULL,
  `id_feature_value` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id_feature`,`id_product`,`id_feature_value`),
  KEY `id_feature_value` (`id_feature_value`),
  KEY `id_product` (`id_product`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The table contains lots of different product features, but I need the features with id_feature_value 4 till 13 and that ID can be used as sort order as well. So far no problem, a simple WHERE clause does the trick:

WHERE fp.`id_feature_value` BETWEEN 4 AND 13 

And the ORDER clause is also straight forward:

ORDER BY fp.`id_feature_value` ASC

But now the tricky bit. Products for which no id_feature_value in the range 4-13 is set, should be merged in as well, but they should be sorted to the end of the list.

And it's this last bit of the query that I cannot wrap my head around. How do I select features within a range and at the same time select features NOT within that range and add a sort order.

2
  • Sounds like an ideal case for UNION. Select the features within the range UNION the features outside the range. Jan 3, 2014 at 16:35
  • I am unsure how a UNION could fix my problem but maybe I am overlooking something. Or maybe I simply don't know enough of all the possibilities of UNION.
    – Mattie
    Jan 3, 2014 at 17:16

2 Answers 2

0

Doesn't this include them?

WHERE fp.`id_feature_value` BETWEEN 4 AND 13
   OR fp.`id_feature_value` IS NULL

EDIT: sorry, i misunderstood what you meant by "for which no ... is set".

I think you want to add something to your SELECT list upon which you can sort, like

IF(fp.`id_feature_value` BETWEEN 4 AND 13, 0, 1) AS my_sort

and then include my_sort ASC in your sort.

simplified example:

mysql> create table so1 (n integer);
Query OK, 0 rows affected (0.11 sec)

mysql> insert into so1 values (1),(2),(3),(4),(5),(NULL);
Query OK, 6 rows affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select n, if(n between 2 and 4, 0, 1) AS s from so1 order by s ASC, n;
+------+---+
| n    | s |
+------+---+
|    2 | 0 |
|    3 | 0 |
|    4 | 0 |
| NULL | 1 |
|    1 | 1 |
|    5 | 1 |
+------+---+
6 rows in set (0.00 sec)
6
  • No that won't work. It is highly unlikely that no feature value at all is set for any product. If I could ad something like OR (fp.id_feature_value BETWEEN 4 AND 13) IS NULL, that would be the solution I guess.
    – Mattie
    Jan 3, 2014 at 17:09
  • sorry, @Mattie - oops. i misunderstood the question. updated. Jan 3, 2014 at 17:14
  • Thanks Rob, but I thought the IF sets the value for my_sort to either 0 or 1 in this example? I need to keep the real value of id_feature_value because that is going to be decisive for the sort order.
    – Mattie
    Jan 3, 2014 at 17:29
  • the idea is to first sort by the 0/1, partitioning the results, then sub-sort by the real value. Jan 3, 2014 at 17:32
  • or if you explicitly don't want the "non-between" partition sorted, make the sort-key something like if(n between 2 and 4, n, 100) and don't sub-sort Jan 3, 2014 at 17:33
0

Here is what I was talking about in my comment: It will automatically give you the results you seek: 4-13 first, then NON 4-13 glommed on at the end of the listing

SELECT
<<query details>>
WHERE fp.`id_feature_value` BETWEEN 4 AND 13 
ORDER BY fp.`id_feature_value` ASC
UNION
SELECT
<<query details>>
WHERE fp.`id_feature_value` NOT BETWEEN 4 AND 13 
ORDER BY fp.`id_feature_value` ASC
1
  • Thanks for the example Chris. I will have to look into the UNION a bit deeper I guess.
    – Mattie
    Jan 3, 2014 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.