How can I write SelectQuery for SELECT LAST_INSERT_ID()? Is it possible to run the following query with SelectQuery ?

SELECT LAST_INSERT_ID();


I would do this with db_query(), but I'm finding a way to use the query builder.

$last_id = db_query("SELECT LAST_INSERT_ID()")-&gt;fetchField();
Although the original query has no table name, db_select() would expect the table name.
The following works perfectly assuming the insert operation took place in another hook and the last insert id is not available into my hook:

$query = db_select('mytable');
$query-&gt;addExpression('LAST_INSERT_ID()', 'last_id');

$result = $query-&gt;execute()-&gt;fetch();
print $query;
echo '&lt;br&gt;';
echo 'Insert ID: '.$result-&gt;last_id;


Query output:

SELECT LAST_INSERT_ID() AS last_id FROM {mytable} mytable