Reputation: 10717
I tried so many times but not any efficent.
My tried 3 queries:
$query = $this->db->query("SELECT COALESCE(links.name, 'noname') as owner FROM links");
$query = $this->db->query("SELECT COALESCE(links.name, users.username) as owner FROM links");
$this->db->select("COALESCE(links.name, 'noname') as owner", false);
2.1.4
5.5.24
I just want show name which not NULL
. If links.name
is NULL, get users.username
But i couldnt figure out.
0 =>
array (size=1)
'owner' => string 'John Doe' (length=9)
1 =>
array (size=1)
'owner' => string '' (length=0)
2 =>
array (size=1)
'owner' => string '' (length=0)
Upvotes: 2
Views: 4052
Reputation: 360872
You're getting empty strings. That means the links.name
field in the record isn't NULL, it's an empty string. coalesce() only triggers if you're actually using a full-blowin SQL NULL
value. Empty strings aren't null, they're just strings that happen to be empty.
Probably this'd work
SELECT IF(links.name='', 'noname', links.name) AS owner
Upvotes: 4