[development] Avoiding sub-queries

Matthew Farina matt at mattfarina.com
Mon Apr 23 10:12:39 UTC 2007


On Apr 22, 2007, at 10:34 PM, Darren Oh wrote:

> I am trying to find the highest node ID in a range of the node  
> table. So far, the only query that works requires a sub-query:
>
>         $nid = db_result(db_query(db_rewrite_sql("
>           SELECT MAX(r.nid)
>           FROM (
>             SELECT n.nid
>             FROM {node} n
>             ORDER BY n.nid
>             LIMIT %d
>           ) r
>         "), $count));
>
> I would prefer for my module to continue working for users stuck  
> with MySQL versions lower than 4.1. Is there any way to rewrite  
> this query so it doesn't use a sub-query?

Instead of db_query try db_query_range so your LIMIT isn't forced to  
mysql.

http://api.drupal.org/api/5/function/db_query_range




More information about the development mailing list