表结构:
CREATE TABLE `weather` (
`id` int(11) NOT NULL,
`recordDate` date DEFAULT NULL,
`temperature` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
表数据:

- inner join
当inner join没有on条件的时候,表现结果和cross join一样
select w1.id as w1_id, w1.recordDate as w1_r, w1.temperature as w1_t, w2.id, w2.recordDate, w2.temperature from weather w1 inner join weather w2;
查询结果:

2. left join 和 inner join 的区别
inner join不会展示匹配条件为NULL的记录,left join 会展示
select w1.id as w1_id, w1.recordDate as w1_r, w1.temperature as w1_t, w2.id, w2.recordDate, w2.temperature from weather w1 join weather w2 on DATEDIFF(w1.recordDate,w2.recordDate)=1;
inner join 结果展示:

select w1.id as w1_id, w1.recordDate as w1_r, w1.temperature as w1_t, w2.id, w2.recordDate, w2.temperature from weather w1 left join weather w2 on DATEDIFF(w1.recordDate,w2.recordDate)=1;

本文探讨了SQL中的INNER JOIN和LEFT JOIN操作。在没有指定ON条件时,INNER JOIN与CROSS JOIN结果相同。INNER JOIN只显示两个表中匹配的记录,而LEFT JOIN会显示左表的所有记录,即使右表没有匹配项。通过示例查询,展示了当JOIN条件为两日期相差一天时,两种JOIN方式的不同结果。
1205

被折叠的 条评论
为什么被折叠?



