gpt4 book ai didi

mysql - LEFT JOIN - 从左表选择中丢失一行

转载 作者:行者123 更新时间:2023-11-30 00:25:38 25 4
gpt4 key购买 nike

我在执行以下查询时遇到问题:

SELECT 
IFNULL(t4.ItemID,
IFNULL(t3.ItemID, IFNULL(t2.ItemID, t1.ItemID))) AS ID,
IFNULL(t4.ParentItemID,
IFNULL(t3.ParentItemID,
IFNULL(t2.ParentItemID, t1.ParentItemID))) as Parent,
IFNULL(tp4.ItemNo, IFNULL(tp3.ItemNo, IFNULL(tp2.ItemNo, tp1.ItemNo))) AS ItemNo
FROM ItemsParents AS t1
LEFT JOIN ItemsDB AS tP1
ON t1.ItemID = tP1.ID
LEFT JOIN ItemsParents AS t2
ON t1.ItemID = t2.ParentItemID
LEFT JOIN ItemsDB AS tP2
ON t2.ItemID = tP2.ID
LEFT JOIN ItemsParents AS t3
ON t2.ItemID = t3.ParentItemID
LEFT JOIN ItemsDB AS tP3
ON t3.ItemID = tP3.ID
LEFT JOIN ItemsParents AS t4
ON t3.ItemID = t4.ParentItemID
LEFT JOIN ItemsDB AS tP4
ON t4.ItemID = tP4.ID
WHERE t1.ParentItemID =
(
SELECT ID
FROM ItemsDB
WHERE ItemNoInt = 359
)

此表应从选择的第一部分返回两行(822 和 875),并从以下 ItemsParents LEFT JOINS 附加更多行。它会附加新行,但原始两行中的一行会丢失。
822
859
834
846
810
...所以第 875 行丢失了
如果我从父项中删除或禁用 LEFT JOIN(通过 "= 1 -- " - 注释掉原始条件):

SELECT IFNULL
(
t4.ItemID,
IFNULL(t3.ItemID, IFNULL(t2.ItemID, t1.ItemID))) AS ID,
IFNULL(t4.ParentItemID,
IFNULL(t3.ParentItemID,
IFNULL(t2.ParentItemID, t1.ParentItemID))) as Parent,
IFNULL(tp4.ItemNo,
IFNULL(tp3.ItemNo, IFNULL(tp2.ItemNo, tp1.ItemNo))) AS ItemNo
FROM ItemsParents AS t1
LEFT JOIN ItemsDB AS tP1
ON t1.ItemID = tP1.ID
LEFT JOIN ItemsParents AS t2
ON t1.ItemID = 1 -- t2.ParentItemID
LEFT JOIN ItemsDB AS tP2
ON t2.ItemID = tP2.ID
LEFT JOIN ItemsParents AS t3
ON t2.ItemID = 1 -- t3.ParentItemID
LEFT JOIN ItemsDB AS tP3
ON t3.ItemID = tP3.ID
LEFT JOIN ItemsParents AS t4 -- t4.ParentItemID
ON t3.ItemID = 1
LEFT JOIN ItemsDB AS tP4
ON t4.ItemID = tP4.ID
WHERE t1.ParentItemID =
(
SELECT ID
FROM ItemsDB
WHERE ItemNoInt = 359
)

...我从左连接表(822 和 875)返回正确的两行。

我用谷歌搜索了左连接表中丢失行的案例,但它们似乎都不适用于这种情况。有什么想法吗?

问候,

伦敦银行同业拆借利率

最佳答案

不是答案,只是想法,我希望您已经要求在左连接中查找表的最后一行,

select LastRowofSubTable.* from tableMain
outer apply
(
select top 1 * from tableSub
where tableMain.id = tableSub.id
order by tableSub.pk_id desc
)as LastRowofSubTable

关于mysql - LEFT JOIN - 从左表选择中丢失一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911387/

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