Was trying to implement this snippet on a customized homepage for "authenticated users" using the Advanced Frontpage module. But keep getting the same error, both with and without the suggested addition to the sql (in the comments of the snippet).

The sql from the snippet is:

SELECT distinct n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($terms) AND n.status=1 ORDER BY n.created DESC

or

SELECT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($terms) AND n.status=1 ORDER BY n.created DESC

Both create the same problem....

The sql sent to the db by Drupal is:

SELECT distinct n.title, DISTINCT(n.nid) FROM tot_node n INNER JOIN tot_term_node tn ON n.nid = tn.nid WHERE tn.tid in (14,15,16,17,18,19,20) AND n.status=1 ORDER BY n.created DESC LIMIT 0, 10

Looks like the core Drupal db function _db_rewrite_sql has automatically determined to insert the DISTINCT() around n.nid, which apparently makes this invalid syntax(?).

The actual error message I get when i try to save the snippet inside my custom homepage is this:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 

'DISTINCT(n.nid) FROM tot_node n INNER JOIN tot_term_node tn ON n.nid = tn.nid WH' 

at line 1 query: 

SELECT distinct n.title, DISTINCT(n.nid) FROM tot_node n INNER JOIN tot_term_node tn ON n.nid = tn.nid WHERE tn.tid in (14,15,16,17,18,19,20) AND n.status=1 ORDER BY n.created DESC LIMIT 0, 10 

in D:\Projects\tot\site\includes\database.mysql.inc on line 172.

How do I fix this?, and, is this a bug I should file?

James Wilson
Lead Developer, Devo Consulting

Comments

jwilson3’s picture

Implementing the snippet in a custom Block also gave the same warning message.

---
James Wilson
Lead Developer, Devo Consulting

jwilson3’s picture

simply changing the order of the selected fields fixes this problem,

EG, this works:

SELECT distinct n.nid, n.title FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE [...] ORDER BY [...]

Whereas, this doesnt:

SELECT distinct n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE [...] ORDER BY [...]

But seems to be very odd "quirk" as it were of the _db_rewrite_sql. My second question still stands.

Is this a bug I should file?

James Wilson
Lead Developer, Devo Consulting

summit’s picture

This worked for me on a php-snipped after using node_acccess.
Thanks for posting!

greetings,
Martijn