1

Currently I have PHP code like this (CodeIgniter):

$detail = $this->db->query("SELECT * FROM tm_survei WHERE ID_SURVEI=5")->row_array();
foreach($detail as &$val){
    if (!!!$val) $val = '-'; //replace all the empty or null values with "-"
}

It solved the problem, but I wonder is it possible to do that in MySQL query itself without that PHP foreach e.g. SELECT IFNULL(*,'-') <original-column-name> FROM tm_survei?

3
  • Maybe this will help: stackoverflow.com/questions/3532776/… Commented Sep 7, 2017 at 9:33
  • You could try the if statement. More info you can find dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html Commented Sep 7, 2017 at 9:34
  • @AlexSlipknot I know about COALESCE() function, but i have no idea how to do it for all columns in one-go, so it's like SELECT COALESCE(firstname,'-') firstname,COALESCE(lastname,'-') lastname,COALESCE(gender,'-') gender FROM tm_survei (one by one). My table have like twenty columns, so it's a waste of code and also unreliable when later the table have new column added or removed. Commented Sep 7, 2017 at 9:41

1 Answer 1

1

IFNULL is really good enough like you suggested, just use SELECT with list of fields to show, not *. Usually, it's ok.

$detail = $this->db->query("SELECT id, title, IFNULL(column_name1, '-'), IFNULL(column_name2, '-') as aliasname FROM tm_survei WHERE ID_SURVEI=5")->row_array();
foreach($detail as &$val){
    $val; 
}
Sign up to request clarification or add additional context in comments.

5 Comments

I once have though about that, then I wonder is there any way to do that for all columns in one go :/ My table have like twenty or more columns (I need them all), so it's a waste of code and also unreliable when later the table have new column added or removed. I still prefer to use my PHP foreach loop to replace the empty values more convenient than mysql IFNULL() one by one for each column.
in future development, if your tables would be that big, you will want to keep your application fast and effective. It means that you will definitely never request all the fields but exactly that you need to display, so you will always list exact fields names. Or, you will use ORM and frameworks instead of this old school SQL+PHP mess.
okay, guess this is the only way :) but this answer is only work for null column, won't work for empty column. The more correct way is to use IF() function, e.g. SELECT IF(FIRSTNAME IS NULL OR FIRSTNAME='','-',FIRSTNAME) FIRSTNAME,IF(LASTNAME IS NULL OR LASTNAME='','-',LASTNAME) LASTNAME,IF(GENDER IS NULL OR GENDER='','-',GENDER) GENDER,IF(DOB IS NULL OR DOB='','-',DOB) DOB,IF(ADDRESS IS NULL OR ADDRESS='','-',ADDRESS) ADDRESS FROM tm_survei WHERE ID_SURVEI=5 anyway, thank you to tell me that replace all empty/null columns in one go is not (maybe not currently) possible.
I have 56 columns, and about 4 of them are in NULL. Do I REALLY have to put all column names in the select? Isn't there something like Select *, IFNULL(Column1)... or something similar?
@Shizukura feel free to use *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.