至少我认为这是这样做的。我不确定如何使用正确的参数调用该程序。
这是SQL过程:
CREATE PROCEDURE catalog_get_products_on_department(
IN inDepartmentId INT, IN inShortProductDescriptionLength INT,
IN inProductsPerPage INT, IN inStartItem INT)
BEGIN
PREPARE statement FROM
"SELECT DISTINCT p.product_id, p.name,
IF(LENGTH(p.description) <= ?,
p.description,
CONCAT(LEFT(p.description, ?),
'...')) AS description,
p.price, p.discounted_price, p.thumbnail
FROM product p
INNER JOIN product_category pc
ON p.product_id = pc.product_id
INNER JOIN category c
ON pc.category_id = c.category_id
WHERE (p.display = 2 OR p.display = 3)
AND c.department_id = ?
ORDER BY p.display DESC
LIMIT ?, ?";
SET @p1 = inShortProductDescriptionLength;
SET @p2 = inShortProductDescriptionLength;
SET @p3 = inDepartmentId;
SET @p4 = inStartItem;
SET @p5 = inProductsPerPage;
EXECUTE statement USING @p1, @p2, @p3, @p4, @p5;
END$$
这是该模式的摘录:
当我尝试使用以下参数调用过程时会发生这种情况:
inDepartmentId: 1,
inShortProductDescriptionLength: 10,
inProductsPerPage: 10,
nStartItem: 1
The following query has failed: "SET @p0='1'; SET @p1='10'; SET @p2='10'; SET @p3='1'; CALL `catalog_get_products_on_department`(@p0, @p1, @p2, @p3); "
MySQL said: #3065 - Expression #1 of ORDER BY clause is not in SELECT list, references column 'turing.p.display' which is not in SELECT list; this is incompatible with DISTINCT
答案 0 :(得分:0)
我认为错误非常明显:
ORDER BY p.display DESC
p.display
不在SELECT
中。这是必需的,因为您有SELECT DISTINCT
。
我认为解决此问题的最简单方法是使用GROUP BY
:
select . . .
from . . .
group by p.product_id, p.name, description,
p.price, p.discounted_price, p.thumbnail
order by max(p.display) desc
答案 1 :(得分:0)
MySQL说:#3065-ORDER BY子句的表达式#1不在SELECT列表中,引用的列'turing.p.display'不在SELECT列表中;这与DISTINCT不兼容
告诉它需要:
turing.p.display
添加到列列表中该语句变为:
SELECT DISTINCT p.product_id, p.name,
IF(LENGTH(p.description) <= ?,
p.description,
CONCAT(LEFT(p.description, ?),
'...')) AS description,
p.price, p.discounted_price, p.thumbnail,
p.display
FROM product p
INNER JOIN product_category pc
ON p.product_id = pc.product_id
INNER JOIN category c
ON pc.category_id = c.category_id
WHERE (p.display = 2 OR p.display = 3)
AND c.department_id = ?
ORDER BY p.display DESC
LIMIT ?, ?";
(请注意添加了新列p.display
或
DISTINCT
这必须消除错误消息,但可以在输出中添加重复项。
ORDER BY
这肯定消除了错误的原因,但是数据的顺序可能会改变甚至变得随机(取决于表/索引的物理组织,版本,月相等)
我认为“ a” 通常是最好的。