Yii queryBuilder;如果没有很好地引用dbexpression

时间:2016-01-19 10:37:40

标签: yii2

我有这个问题:

 $query->select('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa')->from('bud');

如果我执行它会给出synthax错误;我尝试回应正在生成的sql我发现它没有被生成(引用问题),这里是生成的:

select if(bud.posa=2, `1`, `0)` AS `proprietaire`, if(bud.du=1 and bud.gog=1, `1`, `0)` AS `pa` FROM `bud`

0)是导致错误的原因,我不知道如何解决它,任何解决方案?

2 个答案:

答案 0 :(得分:0)

重写这样的查询:

<script>
function PerpendText(name) {
  var val = document.getElementsByName(name)[0];
  val.value = "Invoice Number:" + val.value;  
}

</script>
<form onsubmit="PerpendText('num')">
  Enter invoice number: <input type="text" name="num">
  <input type="submit">
</form>

http://www.yiiframework.com/doc-2.0/yii-db-query.html#select()-detail

答案 1 :(得分:0)

尝试:

use yii\db\Expression; ....... ....... $expresion = new Expresion('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa'); $query->select($expresion)->from('bud');