我有这张桌子:
CREATE TABLE `new_random` (
`i_domain` varchar(500) DEFAULT NULL,
`i_domain_no_http` varchar(500) DEFAULT NULL,
UNIQUE KEY `i_domain_UNIQUE` (`i_domain`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
我已经使用alter table创建了字段i_domain_no_http
。这是新列(此处没有任何值,初始设置为NULL),我想在其中插入i_domain
的确切值,但是在修改它们并删除前缀http://
之后。/
我进行了查询:
insert into `myscheme`.`new_random` (`new_random`.`i_domain_no_http`)
select substring_index(`new_random`.`i_domain`,'http://',-1)
from `myscheme`.`new_random`;
示例,表格应如下所示:
i_domain | i_domain_no_http
------------------------------------
http://11.com | 11.com
https://22.com | 22.com
但是我收到此错误:
i_domain | i_domain_no_http
------------------------------------
http://11.com |
https://22.com |
NULL | 11.com
NULL | 22.com
“ i_domain”应该是主键,而不是NULL,但是我必须删除NN
和PK
以避免错误:
Error Code: 1364. Field 'i_domain' doesn't have a default value
出什么问题了?如何解决?
答案 0 :(得分:0)
您想要的是一个UPDATE
查询:
UPDATE myscheme.new_random
SET i_domain_no_http = SUBSTRING_INDEX(new_random.i_domain,'://',-1)
输出:
i_domain i_domain_no_http
http://11.com 11.com
https://22.com 22.com