【问题标题】:Codeigniter active record CASE working in select but can't order by its parametersCodeigniter 活动记录 CASE 在选择中工作但不能按其参数排序
【发布时间】:2016-10-05 09:45:29
【问题描述】:

我的模型中有以下选择:

$this->db->select("*, product.id as id, product.price as price, attribute.price as att_price, CASE WHEN(attribute.price > 0) THEN attribute.price ELSE product.price END as newprice");

当我获取结果时,我可以回显$product->newprice,它运行良好。问题是我想按产品价格或属性价格(如果存在)来订购结果。我可以通过在order_by() 中使用类似的CASE 轻松使其工作。但是我想跳过它并在那里使用newprice

当我尝试$this->db->order_by("newprice ASC") 时,我得到以下错误:

错误号:105

“where 子句”中的未知列“newprice”

我不明白为什么我不能在 order by 函数中使用newprice,但是当我获取结果时我有 newprice。

【问题讨论】:

    标签: php codeigniter activerecord sql-order-by case


    【解决方案1】:

    只需在 () 中使用CASE WHEN 语句,如下所示,将整个查询放在 temp 中:

    $this->db->select("(*, product.id as id, product.price as price, attribute.price as att_price, (CASE WHEN(attribute.price > 0)                   
    THEN attribute.price ELSE product.price END;) as newprice) as temp");
    $this->db->order_by("temp.newprice ASC")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      相关资源
      最近更新 更多