Mysql教程:(四)连接查询
连接查询
1、左连接查询:
mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu left join score sc on stu.id=sc.id;
注释:stu:为别名。student stu left join score:student:为主表,score为副表显示。 left join:为左连接。 两表关联:其ID必须一一对应(stu.id=sc.id);
2、右连接查询:
mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu right join score sc on stu.id=sc.id;
3、内连接查询:两个表同时都有的内容才会显示。
mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu join score sc on stu.id=sc.id;
4、显示查询数据连接:把后表与前排合起来在一个表显示。
select id,name,class from student union select class,number,maths from score;
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
本文来自博客园,作者:hello_殷,转载请注明原文链接:https://www.cnblogs.com/yinzuopu/p/15516274.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。