zoukankan      html  css  js  c++  java
  • SQL语句大全

    界面工具使用sql语句以查为主,当前以cmd中使用为背景

    1。进入mysql安装路径,如:C:Program FilesMySQLMySQL Server 8.0in

    2。cmd登录mysql:mysql -P 3306 -h localhost -u root -p

    3。输入密码,如:123456

    注:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),需启动Mysql服务

    一、常用查询语句-数据

    1、as关键字

      1)给字段起别名:select id as 序号,name as 名字,age as 年龄 from students;

        2)给表起别名:select s.id,s.name,s.age from students as s;

      

    2、条件语句查询:where后面支持多种运算符,进行条件的处理

      1)比较运算符:=、>、>=、<、<=、!=、<>

        例1:sleect * from students where isdelete=1; 查询已被删除的学生

          例2:select * from students where id>3;   查询编号大于3的学生

           例3:select * from studnets where id <= 4;    查询编号不大于4的学生

         例4:select * from students where name !='黄蓉';  查询姓名不是“黄蓉”;

         2)逻辑运算符 :and、or、not 

         例1:select  * from students where id<3 and gender='女';  查询编号小于3的女同学

          例2:select * from students where name='小龙女' or age=18;  查询姓名为小龙女或年龄为18的学生

         3)模糊查询:like(%任意多个字符;_一个字符)

        例1:select * from students where name like'郭%';  查询姓郭的学生

          例2:select * from students where  name like ‘郭_';  查询姓黄且名是一个字的学生

        4)范围查询:in、between ... and ...

        例1:select * from students where id in (1,6,8);  查询非连续范围:查询编号是1或6或8的学生

          例2:select * from students where id between 1 and 4;  查询一个连续范围:查询编号为1至4的学生

        5)空判断

        例1:select * from students where height is not null;  查询填写了身高的学生

          例2:select * from students where gender is null;  查询没有填写姓名的学生

      

    3、排序

      例1:select * from students where gender='男'  and isdelete=1 order by height desc;  查询删除的男生信息,按身高降序

        例2:select * from students where isdelete=1 order by name;  查询删除学生信息,按名称升序

        例3:select * from students order by height desc,age desc;   先按照身高从高–>矮排序,当身高相同时 按照年龄从大–>小排序

      

    4、分页

    说明
    从start开始,获取count条数据
    start默认值为0
    也就是当用户需要获取数据的前n条的时候可以直接写上  ........limit n;

      例1:select * from students where gender='男' limit 0,3;  查询前三行男生信息

       

    5、聚合函数

      1)总数:count

      count(*) 表示计算总行数,括号中写星与列名,结果是相同的

      select count(*) from students;

        2)最大数:max

      max(列) 表示求此列的最大值

      select max(id) from students where gender='男';  查询男生中,编号最大的值  

       3)最小数:min

      min(列) 表示求此列的最小值

      select min(id) from students where isdelete=1;  查询已删除的学生最小编号值

       4)求和:sum

      sum(列) 表示求此列的和

      例1:select sum(age) from students where gender='男';  查询男生的总年龄

        例2:select sum(age)/count(*) from students where gender='男';  查询男生的平均年龄

       5)平均值:avg

      avg(列) 表示求此列的平均值

      select avg(age) from students where gender='男';  查询男生的平均年龄

      

    6、分组

      1)group by

      select gender from students group by gender;  按性别分组

        2)group by + group_concat()

      group_concat(字段名)根据分组结果,使用group_concat()来放置每一个分组中某字段的集合

        例1:select gender,group_concat(name) from students group by gender; 

          例2:select gender,group_concat(id) from students group by gender;

        3)group by + 聚合函数

      通过集合函数来对这个值的集合做一些操作

        例1:select gender,avg(age) from students group by gender;  分别统计各性别的平均年龄

          例2:select gender,count(*) from students group by gender;  分别统计各性别的人数

        4)group by + having

      having 条件表达式:用来过滤分组结果
      having作用和where类似,但having只能用于group by 而where是用来过滤表数据

      select gender,count(*) from students group by gender having count(*)>2;

        5)group by + with rollup

      with rollup的作用是:在最后新增一行,来记录当前表中该字段对应的操作结果,一般是汇总结果。

      select gender,count(*) from students group by gender with rollup;

      

    7、连接查询

      查询班级表与学生表

        1)内连接:inner join ... on

       例1:select * from students inner join classes on students.cls_id = classes.id;

      

        例2:select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;

       2)左连接:left join ... on

      select * from students as s left join classes as c on s.cls_id = c.id;

        3)右连接:right join ... on

      select * from students as s right join classes as c on s.cls_id = c.id;

       

    8、子查询

    在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句,外部那个select语句则称为主查询.
    主查询和子查询的关系
    子查询是嵌入到主查询中
    子查询是辅助主查询的,要么充当条件,要么充当数据源
    子查询是可以独立存在的语句,是一条完整的 select 语句

      1)标量子查询

      例1:select * from students where age > (select avg(age) from students);

      查询班级学生平均年龄
      查询大于平均年龄的学生

        2)列级子查询

      例2:select name from classes where id in (select cls_id from students);

      查询还有学生在班的所有班级名字

      找出学生表中所有的班级 id;找出班级表中对应的名字

       3)行级子查询

      例3:select * from students where (height,age) = (select  max(height),max(age) from students);

      查找班级年龄最大,身高最高的学生

      

    二、常用增删改-数据

    增加

    说明:主键列是自动增长,但是在全列插入时需要占位,通常使用空值(0或者null) ; 字段默认值 default 来占位,插入成功后以实际数据为准

    1、全列插入:值的顺序与表结构字段的顺序完全一一对应(此时字段名列表不用填写): insert into students values(0,'郭靖',18,180,'女',0);

      2、部分列插入:值的顺序与给出的列顺序对应(此时根据实际的数据填写对应字段列表):insert into students(name,age) values('黄荣',33);

      上面的语句一次可以向表中插入一行数据,还可以一次性插入多行数据,这样可以减少与数据库的通信

    3.全列多行插入:insert into classes values(0,'python'),(0,'java');

      4.部分列多行插入:insert into classes(name) values('JS'),('C++');

    修改

    update students set age=17,gender='男' where id=2;

     删除

    1、delete from students where id=2;

      2、逻辑删除就是修改:update students set isdelete=1 where id=1;

    三、不常用语句

    库的操作

    1、查看所有数据库:show databases;

    2、创建数据库:create database test1 charset=utf8;

    3、删除数据库:drop database test1;

     

    4、进入某个数据库:use guo_test;

    5、查看当前使用哪个数据库:select database();

    6、查看库中所有表:show tables;

      

     表结构操作

    1、创建班级表:create table classes(id int unsigned auto_increment primary key not null, name varchar(10) );

     2、删除表:drop table test;

    3、查看表结构:desc classes;

     4、查看表结构-详细:show create table classes;

      

     修改表字段操作

    1、添加字段:alter table classes add birthday datetime;

     2、修改字段-不可重名:alter table classes modify birth date not null;

     

    3、修改字段-可重名:alter table classes change birthday birth datetime not null;

     4、删除字段:alter table classes drop birth;

  • 相关阅读:
    搭建非域AlwaysOn win2016+SQL2016
    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
    四、基于Windows 2012配置SQL Server 2014 AlwaysOn
    三、安装SQLserver 2014(For AlwaysOn)
    二、 Windows 2012配置故障转移(For SQLServer 2014 AlwaysOn)
    Mybatis-SQL语句构建器类及日志
    Mybatis-JavaAPI
  • 原文地址:https://www.cnblogs.com/ybbybb/p/14337309.html
Copyright © 2011-2022 走看看