mysql-练级查询
mysql的链接查询中主要有五大类链接查询
1.内连接查询
1.1:等值链接查询:指使用等号"="比较两个表的连接列的值,相当于两表执行笛卡尔后,取两表连结列值相等的记录。
SELECT A.*, B.* FROM student_info A inner join student_score B ON A.student_id = B.student_id
1.2:非等值链接查询:指使用大于号">"或小于号"<"比较两个表的连接列的值,相当于两表执行笛卡尔后,取一个表大于或小于另一个表的连结列值的记录
SELECT A.*, B.* FROM student_info A inner join student_score B ON A.student_id > B.student_id
2.外链接查询
2.1左外链接:指将左表的所有记录与右表符合条件的记录,返回的结果除内连接的结果,还有左表不符合条件的记录,并在右表相应列中填NULL
SELECT A.*, B.* FROM student_info A left join student_score B ON A.student_id = B.student_id
2.1又外链接:与左外连接相反,指将右表的所有记录与左表符合条件的记录,返回的结果除内连接的结果,还有右表不符合条件的记录,并在左表相应列中填NULL。
SELECT A.*, B.* FROM student_info A right join student_score B ON A.student_id = B.student_id
3.自然链接查询:指自动将表中相同名称的列进行记录匹配。
SELECT A.*, B.* FROM student_info A natural join student_score B
4.自连接查询:指用表的别名实现表自身的连接。
SELECT B.* FROM student_score A, student_score B WHERE A.student_id = B.student_id AND B.student_score > 80