我找不到与我认为更容易解决的问题相关的解决方案。我有两列数值为INT(5)的列,在第三列中我当然应该获得每一行的差值结果。]
我从此link的加法解决方案中得到提示,但每行返回(无值)。>>
在模型Sistop.php中:
public function getDiff() { $this->diff = 0; if (is_numeric($this->qdsistop) && is_numeric($this->qusistop)) { $this->diff = $this->qdsistop - $this->qusistop; } return $this->diff; }
在SistopSearch.php中
class SistopSearch extends Sistop { public function attributes() { return array_merge(parent::attributes(), ['diff']); } ...... public function search($params) { $query = Sistop::find()->select('*, (`qdsistop` - `qusistop`) AS `diff`'); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); // enable sorting for the related columns $dataProvider->sort->attributes['diff'] = [ 'asc' => ['diff' => SORT_ASC], 'desc' => ['diff' => SORT_DESC], ]; ..... if (is_numeric($this->diff)) { $query->having([ 'diff' => $this->diff, ]); }
结果相同:每行(无值)。
提前感谢。
我找不到与我认为更容易解决的问题相关的解决方案。我有两列的数值为INT(5),在第三列中,我应该得到每个列的差值结果...
与数组输入参数一起使用addSelect
:
$query = Sistop::find();
$query->addSelect(['([[qdsistop]] - [[qusistop]]) AS [[diff]]']);
// more examples:
$query->addSelect(['SUM(amount) AS total_annual', 'IF(val > 0, 1, 0) AS is_selected']);
$query->addSelect(['DATE_FORMAT(date_created,'%Y-%m') AS yymm']);
$query->andWhere("[[diff]] >= :limit", [':limit' => 100]);
$query->addGroupBy(['is_selected', 'yymm']);
$query->orderBy(['diff' => SORT_DESC])