gpt4 book ai didi

MySql:在复杂的 if 语句中更改源表

转载 作者:行者123 更新时间:2023-11-30 00:26:20 25 4
gpt4 key购买 nike

我不确定这是否可行。但我有以下 sql,它根据产品的特价向 product_filter 表插入一些值。

INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
(CASE WHEN price < 100 then 1
WHEN price >= 100 AND price < 500 then 2
WHEN price >= 500 AND price < 1000 then 3
WHEN price >= 1000 AND price < 1500 then 4
WHEN price >= 1500 AND price < 2000 then 5
WHEN price >= 2000 AND price < 2500 then 50
WHEN price >= 2500 AND price < 3000 then 6
ELSE 51 END) AS filter_id
FROM product_special;

我想修改上面的脚本,这样如果没有为产品设置特殊价格,它只会从主 product 表中读取常规价格。由于这将在大型产品数据库上运行,因此我希望可以组合这两个脚本,而不是在某些产品上手动运行不同的脚本。

感谢您的帮助。

最佳答案

INSERT INTO product_filter (product_id,filter_id)

SELECT product_id,
(CASE WHEN price < 100 then 1
WHEN price >= 100 AND price < 500 then 2
WHEN price >= 500 AND price < 1000 then 3
WHEN price >= 1000 AND price < 1500 then 4
WHEN price >= 1500 AND price < 2000 then 5
WHEN price >= 2000 AND price < 2500 then 50
WHEN price >= 2500 AND price < 3000 then 6
ELSE 51 END) AS filter_id
FROM product_special
union
SELECT product_id,
(CASE WHEN price < 100 then 1
WHEN price >= 100 AND price < 500 then 2
WHEN price >= 500 AND price < 1000 then 3
WHEN price >= 1000 AND price < 1500 then 4
WHEN price >= 1500 AND price < 2000 then 5
WHEN price >= 2000 AND price < 2500 then 50
WHEN price >= 2500 AND price < 3000 then 6
ELSE 51 END) AS filter_id
FROM product where product_id not in
(select product_id from product_special)

以上查询假设您有 product_idprice (作为标准/正常价格)product 中的列 table 。这里的想法是从 product_special 获取所有现有产品。表及其特价,并添加 product_special 中不存在的产品。表,但在 product 中表及其标准/常规价格

关于MySql:在复杂的 if 语句中更改源表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22844608/

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