4

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.

  • This is really more of an SQL question than a Drupal question. – tim.plunkett Mar 30 '11 at 19:50
  • @tim to answer this you would need to know drupal's database scheme. It could be reworded to be a pure SQL question, but I don't see any reason migrating this to SO, though it is borderline. – googletorp Mar 30 '11 at 21:23
  • @googletorp I agreed – electblake Mar 31 '11 at 13:34
4
$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 <> 0 AND node.type = '%s' ORDER BY node.nid DESC LIMIT 2)";
  $types[] = $type->type;
}

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

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

print theme_item_list($items);

Execute time: 0.002s

| improve this answer | |
0

This is possible with views, using attachments. However it will not be a single SQL query.

It will however be easier to maintain (as it uses the views GUI) going forward (especially if you have multiple developers in your team).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.