gpt4 book ai didi

mysql - 在单个查询中更新相同的 mysql 字段两次

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

我不确定是否可能,只是想知道是否可以。我的列 plan_popular 具有默认值 0。让我有一个列表:

Plan Name | plan_popular  |  amount
===================================
plan A 0 25.00
plan B 1 50.00
plan C 0 90.00

这就是我正在做的:

$stmt = "update {CI}plans set plan_popular = 0";
$this->db->query($stmt);

$stmt2 = "update {CI}plans set plan_popular = 1 where plan_id = ?";
$this->db->query( $stmt2, array($plan_id) );

现在我已经制定了C计划。现在我想重置它并希望将流行的计划 C 设为 1。我正在做的是运行两个查询,一个是重置并使 plan_popular 0,第二个是更新计划 C到 1 并有 id。是否可以在单个查询中实现?

最佳答案

您可以使用表达式来确定要分配的值:

UPDATE {CI}plans
SET plan_popular = IF(plan_id = ?, 1, 0);

关于mysql - 在单个查询中更新相同的 mysql 字段两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24710528/

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