当前位置: 移动技术网 > IT编程>数据库>Mysql > 数据库中的查

数据库中的查

2018年10月28日  | 移动技术网IT编程  | 我要评论

dml语句

select基本语法

select * from tablename;*指代所有列名的快捷方式

select 列名1,列名2...列名n from tablename;

select 选择列名 from tbname where 表达式;

简单查询,基本格式:

  select 列名1,列名2....

  from 1,表2.....

  where 条件表达式

  group by 分组

  having 分组条件

  order by 列名

  limit num;

选择列

  1. distinct  关键字去重,去除重复数据,紧挨着select

   select distinct dept_id from s_emp;

  1. 简单的算术运算:

    + - * / mod() abs() round()

  2.字符串相关联函数

    concat,lower,upper

   select concat(first_name,' ',last_name) from s_emp;

  3.重命名,起别名

    格式:select 列名1 as 别名1,列名2 as 别名2 from 表:

        select id as d_id from s_emp;

  4.null的显示处理

    ifnull(列名,指定显示值)

     

  5.时间函数

    date_format(curdate(),%y %m %d %h:%i:%s);

    按照指定格式输出时间信息:

        subdate(curdate(),days) 往前数days

       

        adddate(curdate(),30) 往后退30

         

     subtime(curtime(),10) 往前推10

       

     addtime(curtime(),10) 往后退10

       

where 条件

表达式组成:(列名,操作符,数值) and|or|&&|||(列名 操作符 数值)

  1. 算数操作符:

    !=,<> 不等于

    = 判断是否等于

    <=> 判断是否相等:针对于null

  2.逻辑操作符:

  • 列名 between 数值 and 数值:select * from s_emp salsry between 500 and 1000;
  • .列名 not between 数值1 and 数值2:(无穷小,值1或值2到无穷大)
  • in(值1,值2.....)在指定范围内:select id,salary from s_emp where salary in(750,1400,1550);
  • not  in(值1,值2.....)在指定范围外:select count(*) from s_customer where salaes_rep_id not in(11,12) or sales_rep_id <=> null;
  • is null; is not null;
  • like 模糊匹配:通配符%匹配0个或多个,_匹配一个,\转义符;如果是完全匹配建议用“=”效率更高
  • 逻辑连接符:&& and || or,不支持短路求值

例如:找部门编号41,工资大于1000元或者部门编号42,工资小于2000元的员工基本信息;

select id,dept_id,salary from s_emp where (dept_id=41 and salary>1000) or (dept_id=42 && salary<2000);

group by 分组显示

组函数:使用方法:写在having后面

  1. count(*) 计算个数
  2. sum()求和
  3. avg()平均值
  4. max()最大值
  5. min()最小值

having 子句

用于过滤和筛选;分组前使用where进行过滤,分组后使用having进行过滤。

order by排序

asc升序(默认) desc 降序

使用方法:sql语句最后+order by 列名

也可以多字段排序:order by 列名[asc] ; 列名[desc]

limit

限制行数,条数

limit 数字值

子查询

一个select语句的结果,作为另一个select语句的条件:

 

子查询:查询结果是主查询的对象(from)

多表查询

使用连接的方式,进行多表查询

 

等值连接

 

  1. 等值连接,肯定有表与表之间的关联关系(主键,外键,其他用户定义形式);
  2. 通过列名进行连接,列名和列名最好是相同数据类型。

外链接

等值连接(内连接),inner join 获取交集外链接分为三种:

左外连接

 

右外连接

 

全连接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网