select subselect subquery

Events happening in the community are now at Drupal community events on www.drupal.org.
mrharolda's picture

How to use SELECT in a SELECT?

I have a tested and working query that I need to convert to the new Drupal 7 database layer, but I can't seem to find how to use a nested SELECT...

I need to join the MAX() value of last_totalcount to all rows in node_counter and this should be the way to do that:

SELECT nc.nid AS nid, nc.totalcount AS totalcount, last_totalcount AS last_totalcount

FROM node_counter nc, (
  SELECT max(last_totalcount) AS last_totalcount, nid
  FROM node_counter_history GROUP BY nid
) AS tbl

WHERE nc.timestamp >= 1309471200 AND tbl.nid = nc.nid
Read more
Subscribe with RSS Syndicate content