Search code examples
phpmysqlactiverecordyii2

Yii2 ActiveQuery one column not equal another


I need to select all records from DB where а column value is not equal to another one of the same row. In SQL, it looks like this:

SELECT * FROM `user` WHERE (`created_at` != `logged_at`)

In Yii2, I try to do it like that:

$query = User::find()->where(['!=', 'created_at', 'logged_at']);

but it generates

SELECT * FROM `user` WHERE (`created_at` != 'logged_at')

So, the question is, how can I use column name instead of string as the third param?


Solution

  • Try to use yii\db\Expression class:

    $query = User::find()->where(new yii\db\Expression('created_at != logged_at');