自动更改枚举列(Change enum column automatically)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
自动更改枚举列(Change enum column automatically)

我有一个名为status的列,该表是enum类型,可以有2个值1或0 。 我想编写一个查询,它将自动检查Enum的当前值并更改为其他值。

例如,我在表中有行:

id | name | status -----+---------+--------- 1 | Abc | 0 -----+---------+--------- 2 | xyz | 1

现在我想编写一个mysql查询,我将行的id传递给更新,我将自动检查当前状态并将其更改为另一个。 例如,如果我传递id=1 ,它将自动检查状态列的当前值并将其更改为1 。 同样,如果我传递id=2它会检查第2行的状态值并将其更改为0 。

I have a column named status in a table which is of enum type and can have 2 values either 1 or 0. I want to write a query which will automatically check the current value of Enum and change to the other value.

For example, I have rows in table:

id | name | status -----+---------+--------- 1 | Abc | 0 -----+---------+--------- 2 | xyz | 1

Now I want to write a mysql query where I pass the id of row to update and I will automatically check the current status and change it to the other. For example, if I pass id=1 it will automatically check the current value of status column and change it to 1. Similarly, if I pass id=2 it checks the status value of row 2 and changes it to 0.

最满意答案

这里$id是你传递的 id。 请注意,虽然bool操作(如status=!status )是“漂亮的”,但使用ENUM-s这样的方式并不好。

UPDATE t SET status=IF(status='0', '1', '0') WHERE id=$id

另外,好主意是将类型更改为TINYINT如果只是存储0和1 (那么status=!status将是个好主意)

Here $id is your passed id. Note, that while bool operations (like status=!status) is "nifty", it's not good to work with ENUM-s such way.

UPDATE t SET status=IF(status='0', '1', '0') WHERE id=$id

also, good idea would be to change type to TINYINT if it's about storing 0 and 1 only (then status=!status will be good idea to use)

更多推荐

id,status,电脑培训,计算机培训,IT培训"/> <meta name="description" c

本文发布于:2023-04-16 14:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2f12e26ac0e93b708705103c670a90dd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:enum   Change   automatically   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!