I want to build a relative simple select query with a subquery in its condition. What I want to achieve is to get the list of users updated after a given timestamp.
The working SQL-Query which I want to build as dynamic query is:
SELECT uid
FROM `users`
WHERE `vid` > (
SELECT vid
FROM `user_revision`
WHERE `timestamp` > 1379494886
ORDER BY `timestamp` ASC
LIMIT 1
);
I've written the following code to build above query with Drupal's Database API:
$subquery = db_select('user_revision', 'ur')
->fields('ur', array('vid'))
->where('ur.timestamp > 1379494886')
->orderBy('ur.timestamp', 'ASC')
->range(0, 1);
$query = db_select('users', 'u')
->fields('u', array('uid'))
->condition('u.vid', $subquery, '>');
$result = $query->execute()->fetchAssoc();
But, I'm getting the following PDOexception error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT ur.vid AS vid FROM user_revision ur WHERE (ur.timestamp > 137949488' at line 1: SELECT u.uid AS uid FROM {users} u WHERE (u.vid > SELECT ur.vid AS vid FROM {user_revision} ur WHERE (ur.timestamp > 1379494886) ORDER BY ur.timestamp ASC LIMIT 1 OFFSET 0) ; Array ( ) in MYMODULE_page_callback() (line 22 of /path/to/the/module/includes/page_callback_file.inc).
What am I doing wrong building the subquery?