gpt4 book ai didi

带条件列的 Mysql View

转载 作者:行者123 更新时间:2023-11-29 14:18:03 25 4
gpt4 key购买 nike

这一定比我做的要容易,但我在这里要疯了。我想要做的是从三个表创建一个 View ,如下所示:

SELECT tableA.name, tableB.id, tableB.categoryID, tableC.categoryParentID
FROM tableB, tableA where tableA.id=tableB.id JOIN tableC on
tableC.categoryID = tableB.categoryID;

足够简单。但是,我想要完成的是,在 tableB.categoryID 为 NULL 的情况下,我希望 tableC.categoryParentID 显示为 NULL 值而不是categoryParentID 值。

到目前为止,我的结果是,我可以让表显示 tableB.categoryID 不为 NULL 的所有行,并完全排除categoryID 为空值的行 - 我不想这样做 - 或我得到了一些奇怪的变化,其中包括 tableC 中的categoryParentID 的每一行和 tableB 中的每一行 - 这给了我超过 3,000,000 行,而且也是不正确的。

如果不清楚,我可以解释更多。

提前致谢。

最佳答案

如果我理解正确的话,你想要的是:

SELECT tableA.name,
tableB.id,
tableB.categoryID,
tableC.categoryParentID
FROM tableA
JOIN tableB
ON tableB.id = tableA.id
LEFT
OUTER
JOIN tableC
ON tableC.categoryID = tableB.categoryID
;

(参见the "left outer join" section of the Wikipedia article on SQL joins。)

关于带条件列的 Mysql View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com