[英]MySQL SELECT all columns but replace null or empty columns with certain default value
Currently I have PHP code like this (CodeIgniter): 目前,我有这样的PHP代码(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 eg SELECT IFNULL(*,'-') <original-column-name> FROM tm_survei
? 它解决了问题,但我想知道是否可以在没有PHP foreach的MySQL查询本身中做到这一点,例如SELECT IFNULL(*,'-') <original-column-name> FROM tm_survei
吗?
IFNULL is really good enough like you suggested, just use SELECT with list of fields to show, not *. IFNULL确实足够像您建议的那样好,只需将SELECT与要显示的字段列表一起使用,而不要使用*。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.