Jump to content

[SOLVED] IF ... THEN ... ELSE ... END IF


The Little Guy

Recommended Posts

How do I get this if statement to work? I have never used mysql if's before...

 

$query = "SELECT * FROM friends_friends,users 
WHERE
friends_friends.friend = '1'
AND
(friends_friends.myFriend = '$id' OR friends_friends.theirFriend = '$id')
AND
((friends_friends.theirFriend = users.id AND friends_friends.theirFriend != '$id') OR (friends_friends.myFriend = users.id AND friends_friends.myFriend !='$id'))
IF users.displayName != NULL THEN users.displayName = '$letter' 
ELSE users.fname = '$letter' 
END IF
ORDER BY addDate";

 

Error Message:

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 'IF users.displayName != NULL THEN users.displayName = 'j' ELSE users.fname ' at line 8

Link to comment
https://forums.phpfreaks.com/topic/60413-solved-if-then-else-end-if/
Share on other sites

IT WORKS!!!! but.... I heard that IFNULL shouldn't be used on joins.... because it takes way too long

 

From comments section:

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

Don't use IFNULL for comparisons (especially not for Joins)

 

(example:

select aa from a left join b ON IFNULL(a.col,1)=IFNULL(b.col,1)

)

 

It's terrible slow (ran for days on two tables with approx 250k rows).

 

Use <=> (NULL-safe comparison) instead. It did the same job in less than 15 minutes!!

that only seemed to work for displayName, and not fname....

 

There is someone who doesn't have a display name (displayName), and their first name (fname) starts with an M, so I put:

http://publicsize.com/friends/viewFriends?friendID=1&action=letter&lett=m

 

an no results returned, but if they do have a display name, i can get results for them???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.