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);
  1. Users table joined
  2. Tried without join
  3. CodeIgniter version 2.1.4
  4. MySql version 5.5.24

I just want show name which not NULL. If links.name is NULL, get users.username

But i couldnt figure out.

Result is always following

  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)
有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top