今天来介绍一下达梦数据库中SQL的单表查询和多表连接查询部分。这里使用的达梦数据库,数据来源于达梦数据库安装实例时的示例库DMHR,主要使用employee表和department表来做练习。
单表查询
直接使用select字段查询表中某些字段列,或select * 查询所有字段。在达梦中要查询某个表前面需要指定是哪个模式下的表。
select distinct EMPLOYEE.DEPARTMENT_ID from dmhr.EMPLOYEE;
distinct
去重,在达梦中distinct只去重不排序,oracle 的 distinct 去重也排序 结果如下:
使用||连接字段
select a.employee_id,a.employee_name||'的工资是:'||salary FROM DMHR.EMPLOYEE a ;
使用minus、except
test1表 test2表 查找test1表有,test2表没有的数据
select * from dmhr.test1 minus select * from dmhr.test2;
select * from dmhr.test1 except select * from dmhr.test2;
查找test1和test2都有的数据
select * from dmhr.test1 intersect select * from dmhr.test2;
带条件查询
- 比较谓词(=、>、<、>=、<=、<>)
- BETWEEN 谓词
- IN 谓词
- LIKE谓词
- NULL 谓词
- EXISTS 谓词
- max、min、count(*)、AVG、SUM、FIRST_VALUE、AREA_MAX(EXP, LOW, HIGH)
- order by 、group by、rank()、
查找employee表salary字段大于8000小于10000的
select * from dmhr.employee where salary > 8000 and salary < 10000;
select * from dmhr.EMPLOYEE where salary between 8000 and 10000;
查找101,102,103部门编号的员工
select employee_id,department_id from dmhr.employee where department_id in (101,102,103);
like谓词的使用,一般用来进行字符串的匹配,“%''代表任意字符串(也可以是空串);”"代表任何一个字符。如果要匹配%或,则使用*为转义字符 查找员工表中电话号码以15开头,第四个字符为1,结尾为3的员工信息。
select * from dmhr.employee where phone_num like '15_1%3';
like谓词也可以使用.row查找一行中字符串匹配的 查找员工表中每一行只要有出现X的行
select * from dmhr.EMPLOYEE where employee.row like '%X%';
FIRST_VALUE ,返回查询项的第一行记录; AREA_MAX(EXP, LOW, HIGH) 在区间[LOW, HIGH]的范围内取 exp 的最大值。 查找员工表中最低工资 select employee_id,min(salary) from dmhr.EMPLOYEE; 若这样写就会报错,因为employee_id是一个集合,min(salary)是唯一值。 查找各部门员工的最高工资、最低工资、平均工资
select department_id,max(salary) as max_sal,min(salary) as min_sal,avg(salary) as avg_sal from dmhr.employee group by department_id;
在分组函数中使用筛选不能用where,要用having
select department_id,avg(salary) as avg_sal from dmhr.employee group by department_id having avg(salary)>10000;
多表连接查询
多表连接包括内连接和外连接,内连接根据连接产生匹配的数据,外连接又包括左外连接和右外连接、全外连接,左外连接左表数据无论是否匹配全部返回,右外连接右表数据无论是否匹配全部返回,全外连接返回两表中的数据。 内连接
- CROSS JOIN
- natural join
- Joining using
- Join on
查找员工编号、员工姓名、员工部门名称,inner可省略
select employee_id,employee_name,department_name from DMHR.EMPLOYEE a INNER join DMHR.DEPARTMENT b on a.DEPARTMENT_ID=b.department_id;
查询总薪资水平>80000的部门ID、部门名称和部门薪水总和。 错误写法 select department_id,department_name,sum(salary) as max_sal from DMHR.EMPLOYEE join DMHR.DEPARTMENT using (DEPARTMENT_ID) group by DEPARTMENT_ID having sum(salary) >80000; 使用分组时在 select 需要查询的语句中选中的字段,必须出现在 GROUP BY 子句中,前面选择查询department_id,department_name,分组中要以这两列分组,而不能只选择一个字段。
select department_id,department_name,sum(salary) as max_sal from
DMHR.EMPLOYEE join DMHR.DEPARTMENT using (DEPARTMENT_ID)
group by DEPARTMENT_ID,department_name
having sum(salary) >80000;
制造数据,为后面左右外连接使用。 update DMHR.EMPLOYEE t set t.department_id = null where t.employee_id = 1001; commit; insert into dmhr.DEPARTMENT(DEPARTMENT_ID, DEPARTMENT_NAME) values(999999, ‘TEST’); commit; 左外连接
select a.EMPLOYEE_ID, a.EMPLOYEE_NAME, a.DEPARTMENT_ID, b.DEPARTMENT_NAME
from dmhr.employee a left outer join dmhr.department b
on a.DEPARTMENT_ID = b.DEPARTMENT_ID;
右外连接
select b.DEPARTMENT_ID, b.DEPARTMENT_NAME, count(a.employee_id) empnum
from dmhr.employee a right join dmhr.department b
on a.DEPARTMENT_ID = b.DEPARTMENT_ID
group by b.DEPARTMENT_ID, b.DEPARTMENT_NAME;
达梦也兼容oracle 下面写法: select b.DEPARTMENT_ID, b.DEPARTMENT_NAME, count(a.employee_id) empnum from dmhr.employee a, dmhr.department b where a.DEPARTMENT_ID(+) = b.DEPARTMENT_ID group by b.DEPARTMENT_ID, b.DEPARTMENT_NAME;
全外连接
select b.DEPARTMENT_ID, b.DEPARTMENT_NAME, count(a.employee_id) empnum
from dmhr.employee a full join dmhr.department b
on a.DEPARTMENT_ID = b.DEPARTMENT_ID
group by b.DEPARTMENT_ID, b.DEPARTMENT_NAME;
还有更多关于SQL学习请访问达梦学习社区
|