我在以下问题上陷入困境: 我的数据库中有两个表。一个拥有父元素,一个拥有子元素。给出了此结构,不得更改。 我现在必须为每个孩子更新一个字段。该字段可以包含0或1,但不能为NULL。 默认值为0。如果父级保持某种状态,我想将子级类别中的字段更新为1。 到目前为止,我已经尝试过:
UPDATE 'children' AS 'child'
SET 'foo' = (
SELECT 1
FROM 'parents' AS 'parent'
WHERE 'parent'.'type' = "bar" AND 'parent'.'id' = 'child'.'fk_parents_id'
);
这似乎对我不起作用,因为我收到一条错误消息,指出foo
不能为null。
我对SQL还是很陌生,所以我很困在这里。有人可以帮我吗?
答案 0 :(得分:1)
我在这里假设,如果子查询返回children.foo
,则找不到0
字段,因为没有找到具有正确条件的父级。在那种情况下,原始查询失败,因为对于没有匹配父项的行,NULL
部分尝试将SET = (...)
分配给不可为空的字段。
如果两个值直接匹配存在匹配的父级,则查询可以更改为:
NULL
答案 1 :(得分:1)
如果foo不能为NULL。所以你必须测试它是否得到结果并设置另一个值
export default class Product extends Component {
constructor(props) {
super(props);
state = {
comicId: "",
issueObjectState: "",
};
const id = this.state.comicId;
console.log("This is printed from the constructor " + id);
}
}
也从不也永远不要对表或列名称使用仅使用单个qutotes的错误,请参见When to use single quotes, double quotes, and backticks in MySQL
答案 2 :(得分:0)
UPDATE 'children' AS 'child'
SET 'foo' = (
SELECT 1
FROM 'parents' AS 'parent'
WHERE 'parent'.'type' = "bar" AND 'parent'.'id' =
'child'.'fk_parents_id'
)
Where foo >= 0