欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

dmsql

程序员文章站 2022-04-12 23:40:06
DMSQL(select)SQL结构化语言DDL定义createdropalter truncateDML管理select updatedelete insertDCL控制grant revokeTCL事务控制commitrollbackselect简单查询语法:select()from ()第一个括号:*,column_name,alias,expr,||,distinct第二个括号:table n......

DMSQL (select)

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 dmhr.city;

select city_name,city_id from dmhr.city;

select  employee_name,salary as total from dmhr.employee limit 10;

Select employee_name,salary, salary+1000 as total from dmhr.employee limit 10;

    注意:+ - * / ()的优先级。

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

注意:使用连接字符串的时候,字符串只能用单引号。

SQL> select distinct department_id from dmhr.employee;

 

过滤排序

语法:select ()  from ()  where ()

第三个括号:过滤条件

比较运算:

SQL> select employee_name,salary from dmhr.employee where salary>=25000;

select employee_name,salary from dmhr.employee where salary>='25000'; --做了隐式转换

 

>  <  >=  <=  <>  !=

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

 

逻辑运算  and or not

select employee_name,salary from dmhr.employee where employee_name='马学铭' and employee_name='苏国华';

select employee_name,salary from dmhr.employee where employee_name='马学铭' or employee_name='苏国华';

 

模糊查询  like

% 0或多个

_ 代表一个

 

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

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

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

 

IS NULL /IS NOT NULL

select employee_name,salary,commission_pct from dmhr.employee where commission_pct is null limit 10;

IN 枚举

SQL> select employee_name,salary from dmhr.employee where employee_name in ('马学铭','程擎武', '陈仙','强洁芳');

 

SQL> select employee_name,salary from dmhr.employee where employee_name=‘马学铭’ or employee_name=‘程擎武’ or employee_name=‘陈仙’  or  employee_name=‘强洁芳’;

第二种写法会好一些。等价转换后,会把第一条语句转成第二条语句。

 

between…and   介于两者之间,包括边界值

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

等价于 >= and <=

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

 

单行函数

数值函数 round

 

 

 

trunc()

asb 绝对值

mod 取模

 

字符串处理

upper  大写

lower  小写

Initcap  首字母大写

         Instr 查子字符串

                 Instr('helloworld','lo')=4

 substr 截取子字符串

         select substr(email,1,(instr(email,'@')-1)) ,employee_name from employee;

  lpad, rpad  补齐空格

  trim  删除空格

  concat ||

 

日期处理

sysdate  系统当前时间

日期的运算

select  add_days/weeks/months(sysdate,10)

next_day(sysdate,5) 下周四是几号

last_day(sysdate) 本月最后一天

日期和字符的转换

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss');

 

  YYYY  年

  MM  月

  DD   日

  HH   时

  MI    分

  SS    秒

   

 

 

聚合函数

count 计数  count(*) ,count(列名) 忽略空值

SUM 求和

avg  平均

max  最大

min   最小

 

分组统计

统计各部门各有多少人?

SQL> select count(*),department_id from dmhr.employee group by department_id;

对分组之后做筛选。

找出人数最多的部门

SQL> select max(a.tol) from (select count(*) as tol,department_id from dmhr.employee group by department_id) a;

 

排序  

desc  降 

asc 升

 

SQL> select employee_name,salary from dmhr.employee order by salary asc limit 10;

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

 

多表查询

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

内连接 (自然连接)

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

SQL> select e.employee_name,d.department_name from dmhr.employee e left 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 dmhr.employee e right join dmhr.department d on e.department_id=d.department_id limit 10;

把写在 right join 右边的全部显示,左边的只显示满足子连接的,不满足条件的用Null代替。

全外连接

SQL> select e.employee_name,d.department_name from dmhr.employee e full join dmhr.department d on e.department_id=d.department_id limit 10;

 

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

      Select () from () where ()=()

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

 

多行(返回多个值)

All   >all(max)  <all(min)

Any  >any(min)  <any(max)

找出比1002部门工资都高的人?

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

 

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

IN  把子查询都运行完,在运行主查询。

本文地址:https://blog.csdn.net/qq_18743743/article/details/108981891

相关标签: dm达梦数据库

推荐阅读