表结构:
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;