I’m using the following MySQL statement to get Drupal posts where the IDs match across tables:
SELECT node.nid,node_revisions.nid,url_alias.nid, 1, FROM_UNIXTIME(node.created), node_revisions.body, node_revisions.title, node_revisions.teaser, url_alias.slug, FROM_UNIXTIME(node.changed), if(node.type='blog', 'post', 'page')
FROM node, node_revisions, url_alias
WHERE (node.type='blog' OR node.type='page')
AND (node.nid=node_revisions.nid)
AND (node.nid=url_alias.nid)
LIMIT 10;
(note that I’ve done some modifications to url_alias, if anyone is trying to duplicates this)
I have to use two AND statements, because, for some reason, this won’t work:
...
WHERE ...
AND (node.nid=node_revisions.nid=url_alias.nid)
...
Why can’t I compare the IDs all in one AND statement?
SQL doesn’t work like that. MySQL only accepts single pairs of binary comparisons. Most programming languages would behave like you want, but SQL is not a programming language. Writing the joins explicitly will help with the readability of the query and might be a work around for what you’re trying to do. The query could be rewritten like this:
I also change the three
nidcolumns in the select to one since the will always be the same, made the post type condition anINclause since that optimizes better, and made theJOINwithurl_aliasesaLEFT JOINin case a node doesn’t have an alias.