【金沙澳门唯一官网】where关键字的区别,where条

2019-10-26 00:35 来源:未知

创办两张表并插入一些数量

LEFT JOIN 关键字会从左表 (table_name1) 这里回来全体的行,尽管在右表 (table_name2) 中并未有相配的行。

create table class(
  class_id int,
  class_name varchar(20),
  class_grade char(1)
);

insert into class values (1,'语文','A');
insert into class values (2,'数学','B');
insert into class values (3,'英语','C');

create table score(
  class_id int,
  stu_id varchar(20),
  Score int
);

insert into score values (1,'A001',91);
insert into score values (2,'A001',95);
insert into score values (1,'A002',82);
insert into score values (2,'A002',87);
insert into score values (3,'B003',65);

LEFT JOIN 关键字语法

 

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2 
ON table_name1.column_name=table_name2.column_name

翻开表中的数额

讲授:在某个数据库中, LEFT JOIN 称为 LEFT OUTE奥迪Q7 JOIN。

mysql> select * from class;
+----------+------------+-------------+
| class_id | class_name | class_grade |
+----------+------------+-------------+
|        1 | 语文       | A           |
|        2 | 数学       | B           |
|        3 | 英语       | C           |
+----------+------------+-------------+
3 rows in set (0.00 sec)

mysql> select * from score;
+----------+--------+-------+
| class_id | stu_id | Score |
+----------+--------+-------+
|        1 | A001   |    91 |
|        2 | A001   |    95 |
|        1 | A002   |    82 |
|        2 | A002   |    87 |
|        3 | B003   |    65 |
+----------+--------+-------+
5 rows in set (0.00 sec)

mysql>

制造两张表并插入一些数量

 

create table class(
 class_id int,
 class_name varchar(20),
 class_grade char(1)
);
insert into class values (1,'语文','A');
insert into class values (2,'数学','B');
insert into class values (3,'英语','C');
create table score(
 class_id int,
 stu_id varchar(20),
 Score int
);
insert into score values (1,'A001',91);
insert into score values (2,'A001',95);
insert into score values (1,'A002',82);
insert into score values (2,'A002',87);
insert into score values (3,'B003',65);

比较上面几组查询结果
--假设您对left join丰盛熟识的话,先不要看结果,是或不是能够直接揭示上面查询的结果

查看表中的数额

mysql> select * from class A left join score B on A.class_id=B.class_id;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
|        2 | 数学       | B           |        2 | A001   |    95 |
|        1 | 语文       | A           |        1 | A002   |    82 |
|        2 | 数学       | B           |        2 | A002   |    87 |
|        3 | 英语       | C           |        3 | B003   |    65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and 1=1;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
|        2 | 数学       | B           |        2 | A001   |    95 |
|        1 | 语文       | A           |        1 | A002   |    82 |
|        2 | 数学       | B           |        2 | A002   |    87 |
|        3 | 英语       | C           |        3 | B003   |    65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from class A left join score B on 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql> left join的最重要特点是:不管on后面是什么条件,都会返回左表中的所有行!
mysql> select * from class;
+----------+------------+-------------+
| class_id | class_name | class_grade |
+----------+------------+-------------+
|    1 | 语文    | A      |
|    2 | 数学    | B      |
|    3 | 英语    | C      |
+----------+------------+-------------+
3 rows in set (0.00 sec)
mysql> select * from score;
+----------+--------+-------+
| class_id | stu_id | Score |
+----------+--------+-------+
|    1 | A001  |  91 |
|    2 | A001  |  95 |
|    1 | A002  |  82 |
|    2 | A002  |  87 |
|    3 | B003  |  65 |
+----------+--------+-------+
5 rows in set (0.00 sec)
mysql>

 

正如上面几组查询结果

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
|        1 | 语文       | A           |        1 | A002   |    82 |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        2 | 数学       | B           |        2 | A001   |    95 |
|        2 | 数学       | B           |        2 | A002   |    87 |
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        3 | 英语       | C           |        3 | B003   |    65 |
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql>

--倘使你对left join丰富熟练的话,先不用看结果,是还是不是足以一贯表露上边查询的结果

倘诺on后边的标准是左表中的列(and leftTable.colName='***'),左表中满足条件的行和右表中的行进行匹配(根据on leftTable.id=rightTable.id);左表中不满足条件的行,直接出口,其对应的右表中的列都以null。

mysql> select * from class A left join score B on A.class_id=B.class_id;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
|    2 | 数学    | B      |    2 | A001  |  95 |
|    1 | 语文    | A      |    1 | A002  |  82 |
|    2 | 数学    | B      |    2 | A002  |  87 |
|    3 | 英语    | C      |    3 | B003  |  65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and 1=1;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
|    2 | 数学    | B      |    2 | A001  |  95 |
|    1 | 语文    | A      |    1 | A002  |  82 |
|    2 | 数学    | B      |    2 | A002  |  87 |
|    3 | 英语    | C      |    3 | B003  |  65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from class A left join score B on 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

 

mysql> left join的最根本特色是:不管on前面是怎样标准,都会回到左表中的全部行!

mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=65;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        3 | 英语       | C           |        3 | B003   |    65 |
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)

mysql>
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
|    1 | 语文    | A      |    1 | A002  |  82 |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    2 | 数学    | B      |    2 | A001  |  95 |
|    2 | 数学    | B      |    2 | A002  |  87 |
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    3 | 英语    | C      |    3 | B003  |  65 |
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)
mysql>

意气风发经on前边的准则是右表中的列(and rightTable.colName='***'),首先会基于(and rightTable.colName='***')过滤掉右表中不满足条件的行;然后,左表中的行依照(on leftTable.id=rightTable.id)和右表中知足条件的行开展相配。

只要on后边的标准化是左表中的列(and leftTable.colName='***'),左表中满意条件的行和右表中的行举办相配(依照on leftTable.id=rightTable.id);左表中不满足条件的行,间接出口,其对应的右表中的列都以null。

 

mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=65;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    3 | 英语    | C      |    3 | B003  |  65 |
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)
mysql>
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=82;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |     NULL | NULL   |  NULL |
|        2 | 数学       | B           |     NULL | NULL   |  NULL |
|        3 | 英语       | C           |     NULL | NULL   |  NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)

mysql>

若果on前边的尺度是右表中的列(and rightTable.colName='***'),首先会基于(and rightTable.colName='***')过滤掉右表中不满意条件的行;然后,左表中的行依据(on leftTable.id=rightTable.id)和右表中满足条件的行开展相称。

/**********************过滤条件在on中时**********************/
小结一下,假使 left join on leftTable.id=rightTable.id 后还大概有其它规格:
(1)and leftTable.colName='***',过滤左表,不过左表不满意条件的行直接出口,并将右表对应部分置为null
(2)and rightTable.colName='***',过滤右表,对左表未有影响
(3)and leftTable.colName='***' and rightTable.colName='***',正是上边(1)和(2)一同发挥成效

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=82;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |   NULL | NULL  | NULL |
|    2 | 数学    | B      |   NULL | NULL  | NULL |
|    3 | 英语    | C      |   NULL | NULL  | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec)
mysql>

不管on后边有怎么样规范,left join都要回来左表中的全数行!

/**********************过滤条件在on中时**********************/

 

计算一下,假如 left join on leftTable.id=rightTable.id 后还可能有别的标准:

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
|        1 | 语文       | A           |        1 | A002   |    82 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        2 | 数学       | B           |        2 | A001   |    95 |
|        2 | 数学       | B           |        2 | A002   |    87 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        3 | 英语       | C           |        3 | B003   |    65 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育';
Empty set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=90;
Empty set (0.01 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=90;
Empty set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|        1 | 语文       | A           |        1 | A001   |    91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=90;
Empty set (0.00 sec)

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=91;
Empty set (0.00 sec)

mysql> 

(1)and leftTable.colName='***',过滤左表,但是左表不满意条件的行直接出口,并将右表对应部分置为null
(2)and rightTable.colName='***',过滤右表,对左表未有影响
(3)and leftTable.colName='***' and rightTable.colName='***',正是下面(1)和(2)一齐发挥功用

/**********************过滤条件在where中时**********************/

不管on前边有怎样条件,left join都要回去左表中的全体行!

过滤条件写在where中时,先依据where条件对表实行过滤,然后再奉行left join

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
|    1 | 语文    | A      |    1 | A002  |  82 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    2 | 数学    | B      |    2 | A001  |  95 |
|    2 | 数学    | B      |    2 | A002  |  87 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    3 | 英语    | C      |    3 | B003  |  65 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育';
Empty set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=90;
Empty set (0.01 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=90;
Empty set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
|    1 | 语文    | A      |    1 | A001  |  91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=90;
Empty set (0.00 sec)
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=91;
Empty set (0.00 sec)
mysql>

/**********************过滤条件在where中时**********************/

过滤条件写在where中时,先依据where条件对表进行过滤,然后再施行left join

总结

以上所述是小编给我们介绍的sql中的left join及on、where关键字的界别详解,希望对大家有所扶持,假若大家有此外疑问请给小编留言,小编会及时过来大家的。在这里也特别多谢大家对剧本之家网址的支撑!

您大概感兴趣的篇章:

  • 解析sql语句中left_join、inner_join中的on与where的区别
  • 深入分析mysql left( right ) join使用on与where筛选的差距
  • MySQL Left JOIN时内定NULL列重临特定值详解
  • mysql not in、left join、IS NULL、NOT EXISTS 作用难点记录
  • sql 左连接和右连接的采用技术(left join and right join)
  • 超详细mysql left join,right join,inner join用法剖析
TAG标签:
版权声明:本文由金沙澳门唯一官网发布于数据库管理,转载请注明出处:【金沙澳门唯一官网】where关键字的区别,where条