tibberous Posted February 17, 2008 Share Posted February 17, 2008 I have a table called users, with three fields, firstname, lastname and displayname. If displayname isn't null, I want to select displayname, otherwise I want to select firstname, a space, and then lastname. I'm guessing I'll need to use concat, but I'm not sure how to do the actually decision part. Link to comment https://forums.phpfreaks.com/topic/91577-weird-query/ Share on other sites More sharing options...
tibberous Posted February 17, 2008 Author Share Posted February 17, 2008 Crashes =( if users.displayname not null then select display_name from users else select contact(firstname, ' ', lastname) as display_name end if Link to comment https://forums.phpfreaks.com/topic/91577-weird-query/#findComment-469060 Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 Try: SELECT ( IFNULL( displayname, CONCAT_WS( ' ', firstname, lastname )) ) AS display_name FROM users Seems to do the trick. Not bad for a quick test and first try. Link to comment https://forums.phpfreaks.com/topic/91577-weird-query/#findComment-469076 Share on other sites More sharing options...
cunoodle2 Posted February 17, 2008 Share Posted February 17, 2008 Couldn't you just do the decision making in php? if($display_name <> "") { $query = "Select * FROM users WHERE displayname = ".$display_name.";"; } else { $query = "Select * FROM users WHERE displayname = ".".$firstname." ".$lastname.";"; } Link to comment https://forums.phpfreaks.com/topic/91577-weird-query/#findComment-469194 Share on other sites More sharing options...
wildteen88 Posted February 18, 2008 Share Posted February 18, 2008 Couldn't you just do the decision making in php? if($display_name <> "") { $query = "Select * FROM users WHERE displayname = ".$display_name.";"; } else { $query = "Select * FROM users WHERE displayname = ".".$firstname." ".$lastname.";"; } No because the displayname and the firstname/lastname are stored in separate fields within the users table. What the OP wants to do is return the firstname and lastname as the displayname if the displayname is not set within the users table, or just the displayname if it is set. My query does this. Link to comment https://forums.phpfreaks.com/topic/91577-weird-query/#findComment-469824 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.