繁体   English   中英

如何在MySQL上用空字符串替换所有null

[英]How to replace all null with empty String on mysql

美好的一天,

有没有一种方法可以合并一次并将所有可能的空列替换为空字符串或您设置的空列。

假设是这样完成的

select coalesce(col1,'') as col1, coalesce(col2,'') as col2, coalesce(col3,'') as col3 from table1 

有一种方法可以更轻松地执行此操作,因为我必须将大多数查询转换为此,以将空字段替换为“”空字符串。

就像是

select coalesce(*,'') from tablename where col1=1

它看起来确实是错误的。 但是你会明白的

目前我使用laravel查询例如。

 $data = DB::table('table_name)->where('col1',1)->get();

这将转换为"select * from table_name where col1=1";

结果是一个对象数组:

                [{
                        "id": 319,
                        "owner": 830,
                        "name": "new items22",
                        "date_added": "2017-10-05 22:12:59",
                        "last_modified": null,
                        "schedule": 54,
                        "day_index": 0,
                        "day": "Sunday",
                        "type": null,
                        "open": null,
                        "close": null,
                        "special": "closed"
                    },
                    {
                        "id": 320,
                        "owner": 830,
                        "name": "another ITEM",
                        "date_added": "2017-10-05 22:12:59",
                        "last_modified": null,
                        "schedule": 54,
                        "day_index": 1,
                        "day": "Monday",
                        "type": null,
                        "open": "09:00:00",
                        "close": "17:00:00",
                        "special": "open"
                    }]

但我要实现的是代替具有null值,而应将其替换为“”或空字符串。

我建议您在数据库上设置默认值或设置自定义默认值。 我希望这将有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM