gpt4 book ai didi

mysql - SQL问题: inner join gives me empty data set

转载 作者:行者123 更新时间:2023-11-29 06:26:07 24 4
gpt4 key购买 nike

请看一下这个问题 - https://leetcode.com/problems/rising-temperature/

我采用了将表与其自身连接起来的方法,然后放置我的 where 条件来获取我想要的数据。这是我写的查询:-

SELECT wt1.Id FROM Weather wt1 
inner join
Weather wt2 on wt1.id = wt2.id
WHERE wt1.Temperature > wt2.Temperature AND
TO_DAYS(wt1.recordDate)-TO_DAYS(wt2.recordDate)=1;

这给了我一个空数据集。

但是,如果我不使用联接,而仅使用天气表两次,它就可以工作。我的查询:-

SELECT wt1.Id 
FROM Weather wt1, Weather wt2
WHERE wt1.Temperature > wt2.Temperature AND
TO_DAYS(wt1.recordDate)-TO_DAYS(wt2.recordDate)=1;

我想知道内部联接有什么区别以及为什么它不起作用。

最佳答案

您的查询的正确版本(这两个变体实际上都没有执行)是这样的:

SELECT wt1.Id
FROM Weather wt1
INNER JOIN Weather wt2
ON wt1.Temperature > wt2.Temperature AND
TO_DAYS(wt1.recordDate) - TO_DAYS(wt2.recordDate) = 1;

这会根据您的条件使用适当的显式自连接。请注意,wt1.id = wt2.id 连接条件是可疑的,因为假设 id 是主键或唯一列,给定记录永远不会有两个不同的温度值来自同一列。

关于mysql - SQL问题: inner join gives me empty data set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442784/

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