MySQL 按位运算符的状态交换

发布于 2024-08-05 02:19:34 字数 302 浏览 6 评论 0原文

我正在寻找一种解决方案/最佳实践来交换状态标志的值。

mysql 中的状态 INT(3) 有几个值,我希望能够交换 LIVE 和 NOT_LIVE 值,而不中断其中的其他按位值。

如果它是一个标志字段,如 0 或 1,这很容易:

'status' NOT 'status'

我想知道是否有一种方法可以基于两个值 (x, y) 进行交换 - 无需太多代码逻辑,如代码多于。当然,我可能是在做梦,只需恢复到 SELECT 查询、if 语句和 UPDATE...

有什么想法吗?

I am looking for a solution/best practice to do a swap of values for a status flag.

The status INT(3) in mysql has a few values, and I'd like to be able to swap the LIVE and NOT_LIVE values around, without interrupting what other bitwise values are in there.

If it was a flag field, as in 0 or 1, it is easy:

'status' NOT 'status'

I was wondering if there was a way I could do a swap based on two values (x, y) - without too much code-logic, like the code above. Of course I may be dreaming, and just have to revert to a SELECT query, and if statement and an UPDATE...

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-08-12 02:19:34

所以你的值是 1 和 2,但你想要位式翻转?这是行不通的,因为 1 和 2 设置了不同的位!

试试这个:

UPDATE table SET status = CASE status WHEN 1 THEN 2 ELSE 1 END

so your values are 1 and 2, but you want bit-style flipping? that won't work because 1 and 2 have different bits set!

try this:

UPDATE table SET status = CASE status WHEN 1 THEN 2 ELSE 1 END
浮华 2024-08-12 02:19:34

“交换[ping] LIVE 和 NOT_LIVE 值”到底是什么意思?如果 LIVE 是一个已设置/未设置的位,那么您正在寻找的是按位 NAND。例如,如果它的值为 8 并且您想要取消设置它,

UPDATE table SET status = status &~ 8

它将取消设置值为 8 的位。如果 LIVENOT_LIVE 为两个不同的位,那么您需要设置一个(OR)并取消设置另一个(NAND)。在这里,我将在设置 LIVE (8) 时删除 NOT_LIVE (4)

UPDATE table SET status = (status &~ 4) | 8

如果 status 可以只有两个值那么您需要的是将这两个值相加,然后减去status。例如,如果两个值是 1 和 2

UPDATE table SET status = (1 + 2) - status

,当然您可以编写

UPDATE table SET status = 3 - status

注意,如果 status 采用任何其他值,这将失败。如果是两者之一,您可以通过仅更改 status 来避免这种情况。

UPDATE table SET status = 3 - status WHERE status IN (1, 2)

在这种情况下,如果 status 既不是 LIVE 也不是 NOT_LIVE,则不会更改。

What do you mean exactly by "swap[ping] the LIVE and NOT_LIVE values" ? If LIVE is a bit that is set/unset then what you are looking for is a bitwise NAND. For instance, if its value is 8 and you want to unset it

UPDATE table SET status = status &~ 8

It will unset the bit whose value is 8. If LIVE and NOT_LIVE are two different bits then you'll need to set one (OR) and unset the other (NAND). Here I'll remove NOT_LIVE (4) while setting LIVE (8)

UPDATE table SET status = (status &~ 4) | 8

If status can only have two values then what you need is add those two values then substract status. For instance, if the two values are 1 and 2

UPDATE table SET status = (1 + 2) - status

which of course you can write

UPDATE table SET status = 3 - status

Note that this will fail if status takes any other value. You can avoid that by only changing status if it's one of the two.

UPDATE table SET status = 3 - status WHERE status IN (1, 2)

In this case, if status is neither LIVE or NOT_LIVE, it won't be changed.

风向决定发型 2024-08-12 02:19:34

您需要此处的 case 语句

UPDATE table SET status = CASE
    WHEN status = 1 THEN 2
    WHEN status = 2 THEN 3
    ELSE 1
END CASE

或者,您可以使用 if 语句,如果你愿意的话:

UPDATE table SET status =
    IF     status = 1 THEN 2
    ELSEIF status = 2 THEN 3
    ELSE   1
END IF

You need the case statement here:

UPDATE table SET status = CASE
    WHEN status = 1 THEN 2
    WHEN status = 2 THEN 3
    ELSE 1
END CASE

Alternately, you can use the if statement, if you like:

UPDATE table SET status =
    IF     status = 1 THEN 2
    ELSEIF status = 2 THEN 3
    ELSE   1
END IF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文