php - 理解 MySQL IF() 和 ELSE() 时遇到问题

标签 php mysql

我对 PHP IF() 和 ELSEIF() 完全没问题。然而今天我开始使用 MySQL 命令,我有一个很长的 MySQL 查询,我不明白如何修改它。

这是代码:

$db->colums[] = "
IF(
    (SELECT
        IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1,
            IF (distance < deliverymilesto2, deliverysurcharge2,
                IF (distance < deliverymilesto3, deliverysurcharge3,
                    IF (distance < deliverymilesto4, deliverysurcharge4, 
                        IF(distance < deliverymilesto5,deliverysurcharge5,0)
                    )
                )
            )
        ), 0)
        FROM products prod WHERE prod.id_product = p.id_product
    ) > 0,
    (p.price +  (SELECT
        IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1,
            IF (distance < deliverymilesto2, deliverysurcharge2,
                IF (distance < deliverymilesto3, deliverysurcharge3,
                    IF (distance < deliverymilesto4, deliverysurcharge4, 
                        IF(distance < deliverymilesto5,deliverysurcharge5,0)
                         )
                     )
                 )
             ), 0)
             FROM products prod WHERE prod.id_product = p.id_product
        )),
    (p.price)
)as pricedelivery";

我需要做的是如果 p.delivery 等于“2”,则创建一个 ELSE 语句,并添加以下代码:

IF(p.delivery = '2', IF(find_in_set('".$outcoder."', deliveryoutcode1), deliveryoutcharge1,
IF(find_in_set('".$outcoder."', deliveryoutcode2), deliveryoutcharge1, deliveryoutcharge2, 0)), 0)

在 PHP 中这很简单:

if($delivery = '1') {
...code here
} elseif($delivery = '2') {
...otherwise
}

但我就是无法理解如何更改我的声明的逻辑。也许我的大脑很累,因为我今天一直在学习标准化。谁能帮我一下,谢谢。

编辑:我花了整个早上清理 MySQL,其结果如上所示,基于我迄今为止对 MySQL 的了解。

但是,我现在希望能够根据我收到的有用评论,使用 case 重写此代码。我的页面上有 5 个这样的 MySQL 查询,我想知道是否有人可以用 case 格式重写上面的内容,然后我可以用它来重写其他查询?谢谢。

最佳答案

SELECT
  price +
  case delivery
      when '1' then
          case
              when distance < deliverymilesto1 then deliverysurcharge1
              when distance < deliverymilesto2 then deliverysurcharge2
              when distance < deliverymilesto3 then deliverysurcharge3
              when distance < deliverymilesto4 then deliverysurcharge4
              when distance < deliverymilesto5 then deliverysurcharge5
              else 0   
          end
      when '2' then
          case
              when distance < deliverymilesto1 then deliveryoutcharge1
              when distance < deliverymilesto2 then deliveryoutcharge2
              when distance < deliverymilesto3 then deliveryoutcharge3
              when distance < deliverymilesto4 then deliveryoutcharge4
              when distance < deliverymilesto5 then deliveryoutcharge5
              else 0   
          end
      else 0
  end
FROM products

IF() 作为一个函数,与您习惯的分支不同。第一个参数是一个条件,第二个参数是条件为 true 时返回的内容,第三个参数是条件为 false 时的相反情况。

上面的代码只是在第三个参数(else 分支)中使用了大量嵌套来完成您通常使用 IF...ELSE... 或等效地使用 CASE 执行的操作表达。

您必须将其合并到查询的其余部分中,因为您遗漏了其中一些内容。

关于php - 理解 MySQL IF() 和 ELSE() 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28422246/

相关文章:

php - 如何在 ChoiceType Symfony Form Builder 中默认选中一些复选框选项?

javascript - 对 Javascript、PHP 和 Dart 进行基准测试

php - SQLSTATE[42000] : Syntax error or access violation: 1064 sign in form

mysql - 在mysql中的两列之间交换数据

python - 带连接的 Django 查询

php - 使用 PHP 进行认证的随机数生成器

php - 在 woocommerce_loop_add_to_cart_link Hook 后添加Quantity_inputs而不调用它

php - 像 PHP 中的数组一样初始化对象?

iphone - 如何将数据库数据发送到MySQL服务器以更新服务器数据库

php - 取SQL段落和空行(PHP、CSS、$MySQL)