Jump to content

Weird query


tibberous

Recommended Posts

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

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

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.