Single SQL query to pull recent two nodes from each node type As mentioned in the title, I was wondering how this can be achieved with single SQL query. All I need is the node ID from node table that matches the given criteria.
$node_types = node_get_types(); // or create own object of node types
$query = $types = array();

foreach($node_types AS $type){
  $query[] = "(SELECT node.* FROM {node} node WHERE node.status &lt;&gt; 0 AND node.type = '%s' ORDER BY node.nid DESC LIMIT 2)";
  $types[] = $type-&gt;type;
}

$query = implode(' UNION ', $query);
$result = db_query($query, $types);

$items = '';
while($row = db_fetch_object($result)){
  $items[] = l($row-&gt;title, 'node/'. $row-&gt;nid);
}

print theme_item_list($items);


Execute time: 0.002s