各位用户为了找寻关于mysql连接查询中and与where的区别浅析的资料费劲了很多周折。这里教程网为您整理了关于mysql连接查询中and与where的区别浅析的相关资料,仅供查阅,以下为您介绍关于mysql连接查询中and与where的区别浅析的详细内容

1. 建表

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 create table `student`  (   `id` int(11) not null,   `name` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null,   `age` int(11) null default null,   primary key (`id`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;     insert into `student` values (1, '张三', 12); insert into `student` values (2, '李四', 12); insert into `student` values (3, '王五', 12); insert into `student` values (4, '赵六', 12); insert into `student` values (5, '孙七', 12); insert into `student` values (6, '王八', 12); ? 1 2 3 4 5 6 7 8 9 10 11 create table `grade`  (   `id` int(11) not null,   `sid` int(11) null default null,   `grade` int(11) null default null,   primary key (`id`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;   insert into `grade` values (1, 1, 100); insert into `grade` values (2, 2, 80); insert into `grade` values (3, 3, 99); insert into `grade` values (4, 4, 66);

2. inner join-内连接

内连接中and和where没有区别,都是取连接后的结果进行条件筛选。

2.1 不加条件

2.2 and 条件

2.3 where 条件

3. left join - 左外连接

3.1 不加条件

3.2 and 条件

left join中以左表全匹配进行连接,之后使用and进行筛选;不符合条件的左表数据保留,右表数据为null。

3.3 where 条件

在left join 中以左表全匹配进行连接,之后以where进行筛选;只筛选符合条件的数据。

4. right join

同3中left join,只是基表相反。

5. 总结

所有连接,使用where是对连接后符合条件的数据行进行再次的条件筛选,只保留符合条件的数据行; left join连接时,使用and以左表为主,左表数据全部保留,不符合条件的数据行右表数据为null; right join连接时,使用and以右表为主,右表数据全部保留,不符合条件的数据行左表数据为null;

到此这篇关于mysql连接查询中and与where区别的文章就介绍到这了,更多相关mysql连接查询and与where内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/G_x_n/article/details/118314885