公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

对于没有学习过数据库的朋友,各种增删改查操作的语法很是头痛,后续多表查询中内外连接更是头懵,多表联查操作,实际就是在基础的查询语句里不同的位置跟子句进行查询,现以MySQL为例总结一下:

存在学生信息表student:

mysql> select * from student;

| sno | sname | sex | saddress | sage |

| 1 | 李四 | 男 | 上海 | 19 |

| 3 | 王五 | 女 | 湖北武汉 | 18 |

| 4 | 赵六 | 女 | 海南三亚 | 22 |

3 rows in set (0.00 sec))

存在分数表score:

mysql> select * from score;

| id | sid | cid | score |

| 1 | 3 | 1 | 98 |

| 2 | 4 | 1 | 88 |

| 3 | 1 | 1 | 56 |

| 4 | 4 | 2 | 79 |

4 rows in set (0.00 sec)

存在课程表course:

mysql> select * from course;

| courseid | coursename |

| 1 | mysql |

| 2 | linux |

2 rows in set (0.05 sec)

一、where条件后面的子句

等值连接:只返回两个表中连接字段相等的行

举例:查询每个参加了考试的学生信息

mysql> select * from score,student where score.sid=student.sno;

| id | sid | cid | score | sno | sname | sex | saddress |

| 1 | 3 | 1 | 98 | 3 | 王五 | 女 | 湖北武汉 |

| 2 | 4 | 1 | 88 | 4 | 赵六 | 女 | 海南三亚 |

2 rows in set (0.00 sec)

2.非等值连接:做多表连接时使用非’=’号,而使用其它连接运算符的

存在表grade:

mysql> select * from grade;

| id | low | high | rank |

| 1 | 80 | 100 | A |

| 2 | 70 | 79 | B |

| 3 | 60 | 69 | C |

| 4 | 0 | 59 | D |

4 rows in set (0.00 sec)

举例:查询每个分数的等级

mysql> select score,rank from score,grade where score.score between low and high;

| score | rank |

| 98 | A |

| 88 | A |

| 79 | B |

3 rows in set (0.00 sec)

3.自连接:把表虚拟成两个表,自身跟自身进行比较

举例:查询有哪些人比王五大

mysql> select b.sname from student a,student b where b.sage > a.sage and a.sname='王五';

| sname |

| 李四 |

| 赵六 |

2 rows in set (0.00 sec)

4.左连接:外连接中的一种,会显示左表中的所有数据及右表满足条件的数据

举例:查询所有学生的考试情况

mysql> select * from student left join score on student.sno=score.sid;

| sno | sname | sex | saddress | sage | id | sid | cid | score |

| 3 | 王五 | 女 | 湖北武汉 | 18 | 1 | 3 | 1 | 98 |

| 4 | 赵六 | 女 | 海南三亚 | 22 | 2 | 4 | 1 | 88 |

| 4 | 赵六 | 女 | 海南三亚 | 22 | 4 | 4 | 2 | 79 |

| 1 | 李四 | 男 | 上海 | 19 | NULL | NULL | NULL | NULL |

4 rows in set (0.01 sec

5.右连接:外连接中的一表,会显示右表中所有的数据及左表满足条件的数据

举例:查询参数了考试的学生信息

mysql> select * from student right join score on student.sno=score.sid;

| sno | sname | sex | saddress | sage | id | sid | cid | score |

| 3 | 王五 | 女 | 湖北武汉 | 18 | 1 | 3 | 1 | 98 |

| 4 | 赵六 | 女 | 海南三亚 | 22 | 2 | 4 | 1 | 88 |

| 4 | 赵六 | 女 | 海南三亚 | 22 | 4 | 4 | 2 | 79 |

3 rows in set (0.00 sec)

6.子查询:当一个查询是另一个查询的条件时,称之为子查询

举例:查询比王五大的学生

mysql> select sname from student where sage > (select sage from student where sname='王五');

| sname |

| 李四 |

| 赵六 |

2 rows in set (0.00 sec)

二、from后面的子句

派生表:按条件查询结果并派生成临时表,可供查询,但一定话题要带别名

举例:选修了mysql这门课的学生有哪些?

mysql> select * from (select * from score ,course where score.cid=course.courseid and course.coursename='mysql')b ,student a where b.sid = a.sno;

| id | sid | cid | score | courseid | coursename | sno | sname | sex | saddress | sage |

| 1 | 3 | 1 | 98 | 1 | mysql | 3 | 王五 | 女 | 湖北武汉 | 18 |

| 2 | 4 | 1 | 88 | 1 | mysql | 4 | 赵六 | 女 | 海南三亚 | 22 |

2 rows in set (0.00 sec)

三、select后面的子句

举例:计算mysql这门课中的及格率

mysql> select (select count(*) from score,course where coursename='mysql' and score >=60)/(select count(*) from score,course where coursename='mysql')*100 '及格率';

| 及格率 |

| 75.0000 |

1 row in set (0.00 sec)

分享到:
标签:mysql
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定