mysql 从另一个更新 table 错误列不能为空
mysql update from another table error column cannot be null
我有这些 table 结构:
point
===============================================
column | type | comment
===============================================
id | int | primary key (auto increment)
type | enum | 'paradise', 'culinary'
post_id | int | foreign key to culinary.id
user_id | int |
===============================================
culinary
===============================================
column | type | comment
===============================================
id | int | primary key (auto inc)
title | varchar(255) |
user_id | int |
===============================================
我想使用 culinary.user_id
中的值更新 point.user_id
字段。但是我收到一条错误消息说 column 'user_id' cannot be null
即使当我 select 每个 table 时,没有记录具有 null user_id...
这是我尝试过的:
-- first attempt gets error message > 1048 - Column 'user_id' cannot be null
update `point`
set `user_id` = (
select `culinary`.`user_id`
from `culinary`
where `culinary`.`id` = `point`.`post_id`
and `point`.`type` = 'culinary'
)
-- second attempt but still the same error message
update `point`
set `user_id` = (
select `culinary`.`user_id`
from `culinary`
where `culinary`.`id` = `point`.`post_id`
)
where `type` = 'culinary'
使用连接:
update `point` p
inner join `culinary` c on c.`id` = p.`post_id`
set p.`user_id` = c.`user_id`
where p.`type` = 'culinary'
我有这些 table 结构:
point
===============================================
column | type | comment
===============================================
id | int | primary key (auto increment)
type | enum | 'paradise', 'culinary'
post_id | int | foreign key to culinary.id
user_id | int |
===============================================
culinary
===============================================
column | type | comment
===============================================
id | int | primary key (auto inc)
title | varchar(255) |
user_id | int |
===============================================
我想使用 culinary.user_id
中的值更新 point.user_id
字段。但是我收到一条错误消息说 column 'user_id' cannot be null
即使当我 select 每个 table 时,没有记录具有 null user_id...
这是我尝试过的:
-- first attempt gets error message > 1048 - Column 'user_id' cannot be null
update `point`
set `user_id` = (
select `culinary`.`user_id`
from `culinary`
where `culinary`.`id` = `point`.`post_id`
and `point`.`type` = 'culinary'
)
-- second attempt but still the same error message
update `point`
set `user_id` = (
select `culinary`.`user_id`
from `culinary`
where `culinary`.`id` = `point`.`post_id`
)
where `type` = 'culinary'
使用连接:
update `point` p
inner join `culinary` c on c.`id` = p.`post_id`
set p.`user_id` = c.`user_id`
where p.`type` = 'culinary'