mysql - 从另一个表错误列的mysql更新不能为空
问题描述
我有这些表结构:
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 |
===============================================
我想point.user_id
使用来自的值更新字段culinary.user_id
。但我收到一条错误消息,说column 'user_id' cannot be null
即使我选择每个表时,也没有包含 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'
推荐阅读
- bash - 从 while 循环附加到文本文件
- python - LinkedIn REST API 身份验证
- matlab - 如何在MATLAB上解决以下问题
- flutter - 如何在颤动中同时弹出 2 个屏幕
- f# - 在 Giraffe、F# 中使用 JWT 时出现未经授权的错误
- c# - 在c#中的整数集合的每个索引中查找最大值
- vue.js - 无法从“bootstrap-vue\es\components”安装,因为它不包含 package.json 文件
- c# - 如何取消正在运行的hangfire作业?
- swift - OTP 代码视图,自动布局不起作用 - Swift
- javascript - 为什么使用 Puppeteer 和 CodeceptJS 在 Chrome 中无法正确显示页面?