Join query: Design a query of two or more tables at the same time
Join Condition or Join Predicate: A conditional general format used to join two tables:
[<table name1>]<column name1> <comparison operator> [<table name2>]<column name2>
[<table name 1>]<column name 1> between [<table name 2>]<column name 2> and [<table name 2>]<column name 3>
Equijoin:
The concatenation operator is =
Check the status of each student and elective courses
mysql> select student.*, sc.* from student,sc where student.sno=sc.sno; +-------+--------+------+------+-------+-------+-----+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-------+--------+------+------+-------+-------+-----+-------+ | 95001 | Li Yong | Male | 20 | CS | 95001 | 1 | 92 | | 95001 | Li Yong | Male | 20 | CS | 95001 | 2 | 85 | | 95001 | Li Yong | Male | 20 | CS | 95001 | 3 | 88 | | 95002 | Liu Chen | Female | 19 | IS | 95002 | 2 | 90 | | 95002 | Liu Chen | Female | 19 | IS | 95002 | 3 | 80 | | 95004 | Zhang Li | Male | 20 | IS | 95004 | 2 | 65 | | 95004 | Zhang Li | Male | 20 | IS | 95004 | 3 | NULL | | 95004 | Zhang Li | Male | 20 | IS | 95004 | 4 | NULL | | 95005 | Zhang San | Male | 23 | CS | 95005 | 2 | 84 | | 95005 | Zhang San | Male | 23 | CS | 95005 | 4 | NULL | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 1 | 87 | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 2 | 80 | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 3 | 90 | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 4 | 95 | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 5 | NULL | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 6 | NULL | | 96001 | Liu Jun | Male | 30 | IS | 96001 | 7 | 86 | | 97001 | Li Si | Male | 26 | EN | 97001 | 4 | NULL | | 97001 | Li Si | Male | 26 | EN | 97001 | 5 | NULL | +-------+--------+------+------+-------+-------+-----+-------+
Sort:
select [ ] from <表名> order by <列名> [desc/asc];
mysql> select sno, sname from student order by sno; // The default is ascending order +-------+--------+ | sno | sname | +-------+--------+† 12001 | bgg | | 94001 | Cottage| | 95001 | Li Yong| | 95002 | Liu Chen| | 95003 | Wang Min| | 95004 | Zhang Li | | 95005 | Zhang San| | 96001 | Liu Jun| | 96004 | Hibiscus| | 97001 | Li Si| +-------+--------+ mysql > select sno,sname from student order by sno asc; // asc is modified to ascending order +-------+--------+ | sno | sname | +-------+--------+† 12001 | bgg | | 94001 | Cottage| | 95001 | Li Yong| | 95002 | Liu Chen| | 95003 | Wang Min| | 95004 | Zhang Li | | 95005 | Zhang San| | 96001 | Liu Jun| | 96004 | Hibiscus| | 97001 | Li Si| +-------+--------+ 10 rows in set (0.05 sec) mysql > select sno,sname from student order by sno desc; // desc is modified to descending order +-------+--------+ | sno | sname | +-------+--------+ | 97001 | Li Si| | 96004 | Hibiscus| | 96001 | Liu Jun| | 95005 | Zhang San| | 95004 | Zhang Li | | 95003 | Wang Min| | 95002 | Liu Chen| | 95001 | Li Yong| | 94001 | Cottage|† 12001 | bgg | +-------+--------+
mysql> select sno,sname from student order by sno desc,sname asc; //First descend by sno and then ascend by sname
+-------+--------+
| sno | sname |
+ -------+--------+
| 97001 | Li Si | | 96004
| Furong |
| 96001 | Liu Jun |
| 95005 | Min | | 95002 | Liu Chen | | 95001 | Li Yong | | 94001 | Shanzhai | | 12001 | bgg | +-------+--------+ 10 rows in set (0.05 sec)
Selected between operation (closed interval in mysql/mariadb):
select * from <表名> where <列名> between 'a' and 'b';
mysql> select * from student where sno between '94001' and '96001'; +-------+--------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 94001 | Shanzhai | Male | 29 | CS | | 95001 | Li Yong | Male | 20 | CS | | 95002 | Liu Chen | Female | 19 | IS | | 95003 | Wang Min | Female | 19 | MA | | 95004 | Zhang Li | Male | 20 | IS | | 95005 | Zhang San | Male | 23 | CS | | 96001 | Liu Jun | Male | 30 | IS | +-------+--------+------+------+-------+
//not between and
mysql> select * from student where sno not between '94001' and '96001';
+-------+--------+------+------+ -------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------ +-------+ |
12001 | bgg | M | 26 | CS |
| 96004
|
-+-------+------+------+-------+
mysql fuzzy query:
% replaces one or more characters;
_ replaces only one character;
[charlist] any single character in the character list;
[!charlist] or [^charlist] any single character not in the character list;
// Query sno student information starting with 95 mysql> select * from student where sno like ' 95% ' ; +-------+--------+------+- -----+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 95001 | Li Yong | Male | 20 | CS | | 95002 | Liu Chen | Female | 19 | IS | | 95003 | Wang Min | Female | 19 | MA | | 95004 | Zhang Li | Male | 20 | IS | | 95005 | Zhang San | Male | 23 | CS | +-------+--------+------+------+-------+ 5 rows in set (0.05 sec) // Query the student information whose sno ends with 01 mysql> select * from student where sno like ' %01 ' ; +-------+--------+------+- -----+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 12001 | bgg | M | 26 | CS | | 94001 | Shanzhai | Male | 29 | CS | | 95001 | Li Yong | Male | 20 | CS | | 96001 | Liu Jun | Male | 30 | IS | | 97001 | Li Si | Male | 26 | EN | +-------+--------+------+------+-------+ 5 rows in set (0.05 sec)
2018-05-01 20:24:33