达梦数据库sql与mysql_达梦数据库SQL语法

DMSQL

DMSQL 基于sql92 ,部分基于sql99

SQL:结构化的查询语言。

DDL:定义 create drop alter truncate

DML:管理 select update delete insert

DCL:控制 grant revoke

TCL:事务控制:commit rollback

Select

简单查询

语法:select () from ()

第一个括号:*,column_name,alias,expr || distinct

第二个括号:table_name

Select * from city;

Select city_name,city_id from city;

Select city_name cn from dmhr.city

SQL> select employee_name||'的工资是:'||salary as desc1 from dmhr.employee limit 10;

SQL> select distinct department_id from dmhr.employee;

过滤查询

Where 子句常用的查询条件由谓词和逻辑运算符组成,谓词指一个条件,结果为一个布尔值,真,假或是未知。

逻辑运算符:and or not

谓词 包括比较谓词(= > < ,>=,<=,<>),between ,in,like null exists

Like %,_

SQL> select employee_name,salary from dmhr.employee where employee_name like '马%';

SQL> select employee_name,salary from dmhr.employee where salary between 20000 and 30000;

SQL> select employee_name,salary from dmhr.employee where salary>=20000 and salary<=30000;

集函数:

count(*);

相异集函数:AVG|MAX|MIN|SUM|COUNT(DISTINCT);

完全集函数:AVG|MAX|MIN|COUNT|SUM([ALL])

方差集函数:VAR_POP,VAR_SAMP,VARIANCE,STDDEV_POP.....

协方差集函数:COVAR_POP,COVAR_SAMP,CORR;

首行函数 :first_value

求区间范围内最大值:area_max.

字符串集函数:LISTAGG/LISTAGG2

分析函数

引入分析函数后,只需要简单的sql语句,就能实现功能,并且在执行效率方面也有大幅提高。

分析函数

count(*)

方差函数:

var_pop,var_samp,variance,stddev_pop,stddev_samp,stddev

协方差函数

首尾函数:first_value,last_value

分组函数:ntile

相邻函数:lag ,lead

百分比函数:

percent_rank,cume_dist,rtio_to_report,percentile_cont,

字符串函数:listagg

排序

Desc 降序,asc升序

SQL> select employee_name,salary from dmhr.employee order by salary desc;

多表联接查询

语法:select()from () join() on()

第三个括号:表名

第四个括号:关联字段

内连接:根据连接条件,结果集仅包含满足全部连接条件的记录。

SQL> select e.employee_name,d.department_name from dmhr.employee e join dmhr.department d using(department_id) limit 10;

SQL>select e.employee_name,d.department_name from

dmhr.employee e join dmhr.department d on

e.department_id=d.department_id limit 10;

外连接:

左外连接:

把写在left join左边的全部显示,右边的只显示满足条件的,不满足条件的用null代替。

SQL> select e.employee_name,d.department_name from test2.emp e leftjoin test2.dep d on e.department_id=d.department_id limit 10;

右外连接:

把写在right join右边的全部显示出来,左边的只显示满足条件,不满条件的,用null代替。

SQL> select e.employee_name,d.department_name from test2.emp e right join test2.dep d on e.department_id=d.department_id limit 10;

全外连接:返回所有的记录,包括不满足条件的。

SQL> select e.employee_name,d.department_name from test2.emp e full join test2.dep d on e.department_id=d.department_id limit 10;

全外连接=左外连接 union 右外连接

查询两个表的关联列相等的数据用内连接

COL_l 是col_R的子集的时候用右外连接

Col_R 是col_L的子集的时候用左外连接

Col_R 和col_L 彼此有交集,但是彼此不互为子集的时候用全外连接。

分组查询:

语法:select 聚合函数() from () group by () having()

Sum avg max min count

算出各个部门的平均工资?并且将部门平均工资大于10000的找出来。

SQL> select avg(salary) from dmhr.employee group by department_id having avg(salary)>10000;

子查询

子查询的结果是主查询的条件,子查询先于主查询运行。

返回值是唯一的:

Select () from () where () =(子查询结果)

SQL> select employee_name,department_id from dmhr.employee where department_id=(select department_id from dmhr.employee where employee_name='马学铭');

返回值是多行的:

语法:select () from () where ()>|any|all (子查询结果)

ALL: >ALL(MAX)  ANY: >ANY(MIN)  找出比1005部门工资都高的人。

SQL> select employee_name,department_id,salary from dmhr.employee where salary>all(select salary from dmhr.employee where department_id=1005);

找出比1005部门任意一人工资高的人。

SQL> select employee_name,department_id,salary from dmhr.employee where salary >any(select salary from dmhr.employee where department_id=1005);

SQL> select employee_name, salary from dmhr.employee where employee_name in ('戴明','沈连连','常玲');

IN:把子查询运行完,再运行主查询。

Exsits: 先运行子查询,如果有满足条件的,再运行主查询。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值