The Little Guy Posted July 17, 2007 Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted July 17, 2007 Share Posted July 17, 2007 I don't know what makes you think you can use an IF statement a) there and b) with the wrong syntax. IF( expr, if true, if false). Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Author Share Posted July 17, 2007 I can not find the correct syntax, and I don't know where they are supposed to go.... any help? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 I think that form of IF is only used in stored procedures. Try the IF(case,then,else) version. (Are you trying to set those values?) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Author Share Posted July 17, 2007 I am trying to get the value, but if the value for displayName is null, I want to use the column fname instead, because that field is never null IF users.displayName <> NULL THEN users.displayName = '$letter%' ELSE users.fname = '$letter%' END IF Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 Oh, so you want to run a conditional comparison. ...WHERE IFNULL(users.displayName LIKE '$letter%', users.fname LIKE '$letter%')... ...should work. IFNULL(exp1,exp2) returns exp1 if it's not NULL, exp2 otherwise. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Author Share Posted July 17, 2007 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!! Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 Interesting, but that's not what you're doing. <=> is a NULL-safe equality operator; IFNULL() is a conditional statement/function. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Author Share Posted July 17, 2007 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??? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Author Share Posted July 18, 2007 OK... got it, had to make displayName a null field... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.