doumeng3345 2015-10-26 13:30
浏览 48
已采纳

如何将所有空行更改为NULL?

I have a table like this:

// mytable
+----+--------+
| id |  name  |
+----+--------+
| 1  |  Jack  |
| 2  |        |
| 3  |  Peter |
| 4  |        |
| 5  |        |
| 6  |  Alvin |
+----+--------+

Also the output of this query is always 1:

SELECT ... (mytable.name IS NOT NULL) as col ... FROM ...

//=> echo result["col"]; -> always it will be "1"

Now, how can I change empty records to NULL? Because I want it returns 1 just for where there is a name and if there is not any name I want to it returns 0.

Actually I want something like this:

// newtable
+----+--------+
| id |  name  |
+----+--------+
| 1  |  Jack  |
| 2  |  Null  |
| 3  |  Peter |
| 4  |  Null  |
| 5  |  Null  |
| 6  |  Alvin |
+----+--------+

How can I convert my table to this ^ ?

  • 写回答

2条回答 默认 最新

  • dongwei7913 2015-10-26 13:32
    关注

    If your table column name is nullable you can update your data:

    UPDATE mytable
    SET name = NULL
    WHERE name = '';
    

    If your column name is NOT NULL you need to alter it first to allow NULL.

    or if you need to create new table you can use:

    CREATE TABLE newtable
    AS
    SELECT id, CASE WHEN TRIM(name) = '' THEN NULL ELSE name END AS name
    FROM mytable;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型