I have 2 tables:
|users | |users_roles|
-------- -------------
| uid | |uid| rid |
-------- -------------
| 1 | | 1 | 3 |
| 2 |
For the authenticated users the role is missing in users_roles table. User with uid=1 is an admin. I want to write the following query in Drupal DB API:
SELECT
uid
FROM
users
WHERE
NOT EXISTS(
SELECT
NULL
FROM
users_roles
WHERE
users_roles.uid=users.uid
)
I tried with this code, but don't know, how to write the query for notExists condition.
$query = db_select('users');
$query->addField('users', 'uid');
$query_exists = db_select('users_roles');
$query_exists->addExpression('NULL');
$query_exists->condition("users_roles.uid=users.uid"); //<- how to do this. Doesn't work.
$query->notExists($query_exists);
$result = $query->execute();
foreach ($result as $record) {
var_dump($result);
}