I would like to retrieve records in one table not contained in another
I have two tables
tb_users having columns
id(primary key)
username,
firstname,
2. tbl_paychecks
id(primary key)
user_id(foreignkey)
amount
created_at
...
NOw i would like to retrieve all the users without paychecks that it all tbl_users whose id's are not in the tbl_paychecks
So in my users model
public function getNOPaychecks()
{
return self::find()
->leftJoin('tbl_paychecks','`tbl_paychecks`.`user_id` != `tbl_user`.`id`')
->where(['tbl_paycheks.user_id'=>null])
->all();
}
But the above still returns an error of
unknown column tbl_paycheks.user_id in 'where clause
Where am i going wrong?
Not sure why your code is not working.Try doing it as two queries.
public function getNOPaychecks()
{
$paycheckUsers = ArrayHelper::getColumn(PayCheckModelClassName::find()->select(['user_id'])->asArray()->all(),'user_id');
return self::find()
->where(['NOT IN','id',$paycheckUsers])
->all();
}
You have a type in yout table name tbl_paycheks
instead of tbl_paychecks
and for null you could use is
public function getNOPaychecks()
{
return self::find()
->leftJoin('tbl_paychecks','`tbl_paychecks`.`user_id` != `tbl_user`.`id`')
->where(['is', 'tbl_paychecks.user_id', null])
->all();
}