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
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!!

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.