zoukankan      html  css  js  c++  java
  • 数据查询语句

    一、数据查询语句:DQL

      作用:查询

    二、DQL

      #基本查询(可不加where 条件)

      select * from 表名 where 条件;

      select 列1,列2 from 表名 where 条件;

      #过滤掉重复列

      select distinct 列1 from 表名;

      #合并为一行

      select concat (列1,列2) from 表名;

      select concat_ws ('==',列1,列2) from 表名;

      #改名

      slect 列1 as 别名,列名2 as 别名2 from 表名;

      #模糊查询(%匹配所有,_匹配一个)

      select 列名 from 表名 where 列名 like '%xx_';

      #聚合函数(函数中只能是列名或*)

      #表中记录的条数

      select count(*) from 表名;

      #查询此列的和

      select sum(列名) from 表名;

      #查询此列的平均值

      select avg(列名) from 表名;

      #查询此列的最大值

      select max(列名) from 表名;

      #查询此列的最小值

      select min(列名) from 表名;

      #分组查询(having筛选的是分组后的虚拟表格)

      select 列名 from 表名 group by 分组列名;

      select 列名 from 表名 group by 分组列名 having 条件;

      select 列名 from 表名 where 条件 group by 分组列名;

      select 列名 from 表名 where 条件 group by 分组列名 having 条件;

      #多列分组

      select 列1,列2 from 表名 group by 分组1,分组2; 

      #连接查询

      #内连接(inner join)

      例:select s.sno,s.name,c.cno,c.score from atu as s inner join sc as c on s.sno=c.sno;

      #外连接

      #左连接(left join)

      例:select s.sno,s.name,c.cno,c.score from atu as s left join sc as c on s.sno=c.sno;

      #右连接(right join)

      例:select s.sno,s.name,c.cno,c.score from atu as s rigth join sc as c on s.sno=c.sno;

      #联合查询(union:自动去重,union all:不去重)

      select 列1,列2 from 表名1 union select 列1,列2 from 表名2;

      select 列1,列2 from 表名1 union all select 列1,列2 from 表名2;

      #限制查询行数

      #查询3行

      select * from 表名 limit 3;

      #从索引3开始查询5行

      select * from 表名 limit 3,5;

      #排序

      #升序

      select * from 表名 where 条件 order by 列名 asc; 

      #降序

      select * from 表名 where 条件 order by 列名 desc;

      #子查询

      #相关子查询

      例:select sname,sno from stu in (select sno from sc group by sno having avg(score) > 60);

      #非相关子查询

      例:select student.sname,student.sno from student,sc where student.sno = sc.sno and sc.score >= 60 group by sc.sno having count(sc.sno) >= 2;

      #any(>最小值,<最大值)

      select 列名 from 表名 where 条件 > any (select 列名 from 表名 where 条件);

      #all(>最大值,<最小值)

      select 列名 from 表名 where 条件 > all (select 列名 from 表名 where 条件);

  • 相关阅读:
    java程序员从ThinkPad到Mac的使用习惯改变
    关于mybatis缓存配置详解
    log4j每天,每小时产生一日志文件
    $_SERVER常用
    curl抓取页面时遇到重定向的解决方法
    十三)CodeIgniter源码分析之Loader.php
    十二)CodeIgniter源码分析之Model.php
    十一)CodeIgniter源码分析之Controller.php
    十)CodeIgniter源码分析之Output.php
    九)CodeIgniter源码分析之Hook.php
  • 原文地址:https://www.cnblogs.com/badbadboyyx/p/12189132.html
Copyright © 2011-2022 走看看