gpt4 book ai didi

mysql - 使用 MySQL 存储过程进行多列搜索

转载 作者:行者123 更新时间:2023-11-29 00:58:46 25 4
gpt4 key购买 nike

我是 MySQL 存储过程的新手,需要制作一个 MySQL 存储过程来搜索具有多列过滤器的产品列表。如果我不发送所有带值的参数,下面发布的代码将不会返回任何值。如果所有参数都为空,它应该显示所有产品,并在发送其他参数时过滤结果。

DELIMITER $$

USE `537174_alsuper`$$

DROP PROCEDURE IF EXISTS `ListarProductos`$$

CREATE DEFINER=`537174_alsuper`@`%` PROCEDURE `ListarProductos`(
IN Token VARCHAR(50),
IN cID INT(11),
IN pID INT(11),
IN os INT(1),
IN productName VARCHAR(255)
)
BEGIN

DECLARE BaseToken VARCHAR(50);
SELECT TOKEN_VALUE INTO BaseToken FROM web_service_tokens WHERE 1 ORDER BY TOKEN_ID DESC LIMIT 1;

IF (Token = BaseToken) THEN

SELECT DISTINCT d.thumbnail, a.productID, a.name_en, a.categoryID, a.Price, a.description_en
FROM SC_products a, SC_product_pictures d
WHERE a.ordering_available='1' AND d.productID=a.productID AND

CASE
WHEN cID IS NOT NULL
THEN a.categoryID = cID
WHEN cID IS NULL
THEN 1 = 1
END

AND

CASE
WHEN pID IS NOT NULL
THEN a.productID = pID
WHEN pID IS NULL
THEN 1 = 1
END

AND

CASE

WHEN os IS NOT NULL
THEN a.on_special = os
WHEN os IS NULL
THEN 1 = 1
END

AND

CASE
WHEN productName IS NOT NULL
THEN a.name_en LIKE CONCAT('%',productName,'%')
WHEN productName IS NULL
THEN 1 = 1
END


ORDER BY a.name_en ASC;

END IF;

END$$

DELIMITER ;

谁能指出使用多列过滤器进行存储过程搜索的正确方法?

最佳答案

这个怎么样:

SELECT DISTINCT d.thumbnail, a.productID, a.name_en, a.categoryID, a.Price, a.description_en
FROM SC_products a
INNER JOIN SC_product_pictures d ON d.productID = a.productID
WHERE a.ordering_available='1' AND
(cID IS NULL OR a.categoryID = cID) AND
(pID IS NULL OR a.productID = pID) AND
(os IS NULL OR a.on_special = os) AND
(productName IS NULL OR a.name_en LIKE CONCAT('%', productName, '%'))

关于mysql - 使用 MySQL 存储过程进行多列搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com