Hello everyone, I've been happily chugging along, trying to get my entire Drupal site going using PHP5 and PostGres 8.x

I've finally run into something that I don't think is a bug, but it sure seems illogical to me, and I can't figure out what the statement is trying to accomplish, so therefore I can't seem to convert it into a compliant SQL statement.

The following code is generated by the forums module.

SELECT n.nid, l.last_comment_timestamp, IF(l.last_comment_uid, cu.name, l.last_comment_name) as last_comment_name, l.last_comment_uid FROM node n, node_comment_statistics l, users cu, term_node r WHERE n.nid = r.nid AND r.tid = 2 AND n.status = 1 AND n.type = 'forum' AND l.last_comment_uid = cu.uid AND n.nid = l.nid ORDER BY l.last_comment_timestamp DESC LIMIT 1 OFFSET 0;

What I don't understand is the purpose of the IF statement in there, that doesn't appear to be anything at all SQL related, but who knows maybe it's a mysql thing.

Any idea what this query is asking the database to do?

Thanx

Comments

smorrey’s picture

Hello, everyone.

I believe removing the

, IF(l.last_comment_uid, cu.name, l.last_comment_name)

From the 2 places it exists in forums.module fixed the error I was getting.

Things appear to be normal, however I'm still not sure exactly what that statement was trying to accomplish. Maybe it's just me but that didn't seem like it should work on any DB. Anyways, if removing it may have some repurcussions I can't forsee, please let me know so I can find some other work-around.

Thanx

nevets’s picture

The if statement determines what data is returned as last_comment_name. If can be read as

IF l.last_comment_uid is TRUE
 use the value of cu.name for last_comment_name
ELSE 
 use the value of cu.name for last_comment_name 

Or in english in the context of which it is used, if the comment was posted by someone logged on, use their user name, otherwise use the name they provided when they made the comment.

smorrey’s picture

So this was redundant then. It returns the exact same fields whether true or false according to your above statement?

Eitherway, only logged in users can post comments on my site, so this will hopefully be a non-issue.

Anyways thanks for the reply.

nevets’s picture

Sorry that should have read
The if statement determines what data is returned as last_comment_name. If can be read as

IF l.last_comment_uid is TRUE
 use the value of cu.name for last_comment_name
ELSE
 use the value of l.last_comment_name for last_comment_name

Or in english in the context of which it is used, if the comment was posted by someone logged on, use their user name, otherwise use the name they provided when they made the comment.