对于熟悉Prestashop的人,我试图在类别视图中添加额外的排序顺序选项。更具体地说,我想为选择的功能添加额外的排序顺序。
这是获取产品的prestashop查询的主要部分:( SELECT部分的最后一列以及我添加的最后一个JOIN)
$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';
表格ps_feature_product
如下所示:
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;
该表包含许多不同的产品功能,但我需要id_feature_value
4到13的功能,并且该ID也可以用作排序顺序。
到目前为止没问题,一个简单的WHERE子句可以解决这个问题:
WHERE fp.`id_feature_value` BETWEEN 4 AND 13
ORDER条款也很简单:
ORDER BY fp.`id_feature_value` ASC
但现在是棘手的一点。
设置了4-13范围内没有id_feature_value
的产品也应该合并,但它们应该排序到列表的末尾。
这是查询的最后一点,我无法解决这个问题。 如何选择范围内的要素并同时选择不在该范围内的要素并添加排序顺序。
答案 0 :(得分:0)
这不包含它们吗?
WHERE fp.`id_feature_value` BETWEEN 4 AND 13
OR fp.`id_feature_value` IS NULL
编辑:对不起,我误解了你的意思是“没有设置......”。
我认为您希望在SELECT
列表中添加一些可以对其进行排序的内容,例如
IF(fp.`id_feature_value` BETWEEN 4 AND 13, 0, 1) AS my_sort
然后在您的排序中加入my_sort ASC
。
简化示例:
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)
答案 1 :(得分:0)
以下是我在评论中谈到的内容: 它将自动为您提供所寻求的结果:首先是4-13,然后是在列表末尾的非4-13点亮
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