1. 查询

检索所有行和列

select * from table

select * from employee

查询部分列

select col1,col2,col3 from table

select fname,mint from employee

使用友好列标题查询(更换查询出来后的表头名称)

select col1 as1,col2 as2 from table

select fname as 名字,mint as 代号 from employee

检索前n行数据

-- SQL Server
select top n * from table

select top 3 * from employee
-- MySQL
select * from table limit 0,n

select * from employee limit 0,3

检索前%n行数据

指定检索条件

区间查询

筛选1列或多列不重复的数据

模糊查询

查询null数据

对查询的数据进行排序

算数表达式

函数

数据分组(group by)

group by 对原始数据进行分组 having 对分组后的数据进行筛选(先有group by 后having)

多表查询

链接查询分类

  • 内连接

    ```sql

    -- 查询每个员工的工作时长

    SELECT employee.fname,employee.address,sum(works_on.hours) FROM works_on JOIN employee ON works_on.essn=employee.ssn GROUP BY fname;

SELECT dname,dnumber,COUNT(dno) FROM department JOIN employee on dno=dnumber GROUP BY dname;

SELECT fname,dname FROM employee,department WHERE dno=dnumber

  • 右外连接

  • 全外连接

    • 交叉连接

嵌套查询

In

all

exists

集合查询

并运算

前提条件:select的列数目相等且类型兼容

  • union 剔除重复记录

  • unionall 保留重复记录

交运算

差运算

Last updated

Was this helpful?