基于 MySQL 的数据库实践(扩展运算)
Posted wander4096
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于 MySQL 的数据库实践(扩展运算)相关的知识,希望对你有一定的参考价值。
select 中的通配符
星号 *
可以用在 select
子句中表示所有的属性。
mysql> select instructor.*
-> from instructor, teaches
-> where instructor.ID = teaches.ID;
+-------+------------+------------+----------+
| ID | name | dept_name | salary |
+-------+------------+------------+----------+
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 12121 | Wu | Finance | 90000.00 |
| 15151 | Mozart | Music | 40000.00 |
| 22222 | Einstein | Physics | 95000.00 |
| 32343 | El Said | History | 60000.00 |
| 45565 | Katz | Comp. Sci. | 75000.00 |
| 45565 | Katz | Comp. Sci. | 75000.00 |
| 76766 | Crick | Biology | 72000.00 |
| 76766 | Crick | Biology | 72000.00 |
| 83821 | Brandt | Comp. Sci. | 92000.00 |
| 83821 | Brandt | Comp. Sci. | 92000.00 |
| 83821 | Brandt | Comp. Sci. | 92000.00 |
| 98345 | Kim | Elec. Eng. | 80000.00 |
+-------+------------+------------+----------+
15 rows in set (0.01 sec)
它返回了 instructor 中所有的属性。
元组的排序
SQL 为用户提供了一些结果显示的顺序的控制,order by
子句可以让查询结果中元组按升序显示,考虑按字典序列出 Physics 系的所有教师。
mysql> select name
-> from instructor
-> where dept_name = ‘Physics‘
-> order by name;
+----------+
| name |
+----------+
| Einstein |
| Gold |
+----------+
2 rows in set (0.00 sec)
为了使用降序,我们可以用 desc
表示降序,完整地说,可以用 asc
表示升序,此外,排序可以在多个属性上进行,例如我们希望按 salary 的降序列出整个 instructor 关系,如果有几名教师工资相同,则按姓名升序排列。
mysql> select *
-> from instructor
-> order by salary desc, name asc;
+-------+------------+------------+----------+
| ID | name | dept_name | salary |
+-------+------------+------------+----------+
| 22222 | Einstein | Physics | 95000.00 |
| 83821 | Brandt | Comp. Sci. | 92000.00 |
| 12121 | Wu | Finance | 90000.00 |
| 33456 | Gold | Physics | 87000.00 |
| 98345 | Kim | Elec. Eng. | 80000.00 |
| 76543 | Singh | Finance | 80000.00 |
| 45565 | Katz | Comp. Sci. | 75000.00 |
| 76766 | Crick | Biology | 72000.00 |
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 58583 | Califieri | History | 62000.00 |
| 32343 | El Said | History | 60000.00 |
| 15151 | Mozart | Music | 40000.00 |
+-------+------------+------------+----------+
12 rows in set (0.00 sec)
where 子句谓词
为了简化 where
子句,SQL 提供 between
运算符来说明一个值落在一个闭区间内。
考虑查询,找出工资在 90000 美元和 100000 美元之间的教师的姓名,直观地有
mysql> select name
-> from instructor
-> where salary <= 100000 and salary >= 90000;
+----------+
| name |
+----------+
| Wu |
| Einstein |
| Brandt |
+----------+
3 rows in set (0.01 sec)
可以用 between
运算符改写为
mysql> select name
-> from instructor
-> where salary between 90000 and 100000;
+----------+
| name |
+----------+
| Wu |
| Einstein |
| Brandt |
+----------+
3 rows in set (0.01 sec)
类似的有 not between
运算符
mysql> select name
-> from instructor
-> where salary not between 90000 and 100000;
+------------+
| name |
+------------+
| Srinivasan |
| Mozart |
| El Said |
| Gold |
| Katz |
| Califieri |
| Singh |
| Crick |
| Kim |
+------------+
9 rows in set (0.00 sec)
where
子句还支持在元组上进行比较,考虑查询,查找 Biology 系讲授了课程的所有教师的姓名和他们所讲授的课程
mysql> select name, course_id
-> from instructor, teaches
-> where (instructor.ID, dept_name) = (teaches.ID, ‘Biology‘);
+-------+-----------+
| name | course_id |
+-------+-----------+
| Crick | BIO-101 |
| Crick | BIO-301 |
+-------+-----------+
2 rows in set (0.00 sec)
以上是关于基于 MySQL 的数据库实践(扩展运算)的主要内容,如果未能解决你的问题,请参考以下文章