mysql> explain select a.*,b.*from student a rightjoin exams b on a.uid = b.uid; +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ |1| SIMPLE | b |NULL|ALL|NULL|NULL|NULL|NULL|12|100.00|NULL| |1| SIMPLE | a |NULL| eq_ref |PRIMARY|PRIMARY|4| school.b.uid |1|100.00|NULL| +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ 2rowsinset, 1 warning (0.00 sec)
mysql> explain select a.*,b.*from student a leftjoin exams b on a.uid = b.uid; +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|6|100.00|NULL| |1| SIMPLE | b |NULL|ref|PRIMARY|PRIMARY|4| school.a.uid |2|100.00|NULL| +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ 2rowsinset, 1 warning (0.00 sec)
mysql>
现在有这么一个情景:我们要查询没有考过试的学生
我们可以使用子连接
1
select*from student where uid notin (select uid from exams);
1 2 3 4 5 6 7 8 9
mysql>select*from student where uid notin (select uid from exams); +-----+--------+-----+-----+ | uid | name | age | sex | +-----+--------+-----+-----+ |6| weiwie |20| M | +-----+--------+-----+-----+ 1rowinset (0.00 sec)
mysql>select a.*from student a leftjoin exams b on a.uid = b.uid where b.cid isnull; +-----+--------+-----+-----+ | uid | name | age | sex | +-----+--------+-----+-----+ |6| weiwie |20| M | +-----+--------+-----+-----+ 1rowinset (0.01 sec)
mysql> explain select a.*from student a leftjoin exams b on a.uid = b.uid where b.cid isnull; +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|6|100.00|NULL | |1| SIMPLE | b |NULL|ref|PRIMARY|PRIMARY|4| school.a.uid |2|10.00|Usingwhere; Notexists; Using index | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ 2rowsinset, 1 warning (0.01 sec)
mysql>
查看没有参加课程号为3考试的学生
1 2 3
select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3 where b.cid isnull;
1 2 3 4 5 6 7 8 9 10 11
mysql>select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3where b.cid isnull; +-----+----------+-----+-----+ | uid | name | age | sex | +-----+----------+-----+-----+ |1| zhangsan |18| M | |4| linfeng |21| W | |6| weiwie |20| M | +-----+----------+-----+-----+ 3rowsinset (0.01 sec)
mysql>
explain 查询
1 2 3 4 5 6 7 8 9 10 11 12
mysql> explain select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3where b.cid isnull; +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|5|100.00|NULL | |1| SIMPLE | b |NULL| eq_ref |PRIMARY|PRIMARY|8| school.a.uid,const |1|100.00|Usingwhere; Notexists; Using index | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ 2rowsinset, 1 warning (0.00 sec)