Hide/Don't fetch 'users' if they are present in another table.

Hi all, Good day!

I want to fetch all users from tbl_user except those who are already present in another table - tbl_group_user. Equivalent of

SELECT * FROM tbl_user as u, tbl_group_user as gu WHERE u.id != gu.user_id AND gu.group_id = 2

I will be using CGridView to display the results where admin can select one or multiple user(s) and assign them to the selected group.

Any help is greatly appreciated.



Had to do a couple of operations…

            $groupUsers = GroupUser::model()->findAllByAttributes(array('group_id' => $selectedGroup->id));

            $userIds = array();

            foreach ($groupUsers as $groupUser) {

                $userIds[] = $groupUser->user_id;


            $criteria = new CDbCriteria();

            $criteria->addNotInCondition('id', $userIds);

            $dataProvider = new CActiveDataProvider('User', array('criteria' => $criteria));

Would like to know if there is a better approach.