gpt4 book ai didi

mysql - SQL存储过程和位运算符

转载 作者:行者123 更新时间:2023-11-29 07:07:51 26 4
gpt4 key购买 nike

我有一个类别表:

    id;description;special
----------------------
1;Spares;TRUE
2;Accessories;TRUE
4;Consumables;TRUE
8;Services;TRUE
11;Printer;FALSE
12;Monitor;FALSE
13;Other;FALSE

特殊字段指定特殊类别,具有固定的id,不能删除也不能修改。我已经为这些分配了按位 OR-able id。

然后我有项目,每个项目都属于一个类别(具有 1:n 关系)。

现在我想编写一个存储过程,它接受一个包含 ID 的 OR 运算组合的输入参数:

1 I want spare parts
2 I want accessories
4 I want consumables
**5 I want consumables AND spare parts**

等等

如果参数为 NULL,那么我想要每个项目,而不考虑其类别。

这很简单,假设参数名为 _or_category,那么 WHERE 子句可能是这样的:

SELECT
*
FROM
items I
JOIN
categories C ON (C.id = I.category)
WHERE
(_or_category IS NULL) OR (C.special = TRUE AND C.id | _or_categoria = _or_categoria)
;

第一个问题:*编辑:抱歉这不是问题,因为我在 WHERE 子句中有 C.special=TRUE。*类别 12 可以“看到”为 id=8 或 4,因此如果我只想选择消耗品,我也会选择显示器!

第二个问题:我不知道如何指定何时需要所有不是服务的项目(类别:8)。

最佳答案

Second problem: I don't know how to specify when I want all the items which are NOT a service (cat: 8)

如果我理解你的问题,我认为你正在寻找按位反转位 ~

例如

C.special = TRUE AND  (~C.ID | or_categoria = _or_categoria)

关于mysql - SQL存储过程和位运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253875/

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