• MySQL-零基础开发


    1.终端下连接mysql服务

      mysql -uroot -p回车后输入设定的密码即可。

    进去后每条命令结尾要带分号;退出命令exit  

    单行注释有两种:#  或 --空格。多行注释/*  */

     2.基本命令集合

    针对数据库:use sys;  show databases;  

        查看当前操作的数据库:select databse();

    针对表:1.创建表eg1示例:create table  eg1(      #此时回车

          stuid  int,

          stuname varchar(20),

          gender   char,    #代表单个字符

          borndate  datetime);  #命令结尾时才带分号

    2.  desc  eg1;    #查看表的描述  describe

       select * from eg1;  #查看表中所有字段数据,新创建的表应返回为Empty set

        insert into eg1 values(1,'张三','男','1999-6-6');  #向表中插入数据,注意使用英文逗号 

       insert into eg1 values(2,'李四','男','1999-6-6'); 

    #若提示格式不对,set names utf8;修改my.ini里的utf8为gbk

      

    update eg1 set borndate='2020-02-02' where stuid=2;   #更新/修改表中的数据,如果没有后面的where,会更新整列数据
    
      delete from eg1 where stuid=1;  #删除数据
    
      alter table eg1 add column email varchar(20);  #修改表的结构,添加列   
    
      drop table eg1;  #删除整个表

    3.基础查询

    查询结果是个虚拟表不能直接操作数据。

      select   #  常量  表达式(这两个不用写来自哪个表)  函数 字段。对于来自哪个表可以双击表头,将自动用着重符·填写。不是关键字的可以不加着重符号

    F12键 可对齐命令。

    select  version()  #查询版本

    select   user()  #查询用户

    起别名

      select user() as 用户名;  #as 也可以省略为空格

      select user() as '用  户名';  #包含了空格,避免使用查询时出现语法错误

      select user() as "用户   名";

    例如:select  last_name  as  "姓   名"  from table;  #若不使用引号,会出现语法错误。

      + 在mysql中作为运算符时,字符型强制转换为整形失败,则默认为0。其中一个操作数为null时,null+null=null=null+12=null

    字段拼接查询

      select  concat(字段1,字段2) as "新  字  段"  from  table;

    去重查询:  select distict 字段  from table;

     显示全部列,各个列用逗号连接,列头显示为out_put:

      select concat(字段1,','字段2,','字段3) as 新字段 from table;

      ifnull(表达式1,表达式2)  #如果表达式1为null,显示结果为表达式2.

    避免查询出null:  select concat(字段1,','字段2,','ifnull(字段3,'')) as 新字段 from table;

    4.条件查询

    select 查询列表
    
    from table
    
    where 筛选条件; 

    执行顺序为:from->where->select

    select * from table where id<>100;  #查询id不等于100的信息。

    select * from table where not(id>=20 and id<= 60);  #查询id小于20大于60的信息。虽然可以用!代替not,  &&代替and  但是不建议,这样不专业。

    模糊查询:like 一般和通配符_(单个字符)  %(多个字符)

      select * from table where like '%条件%'  #查询包含  条件  的信息。

      查询下划线_:  '$_%' escape '$';  #escape 使$符号 变为使转义字符,相当于  不过不建议,不炫。

      in (常量表达式1,常量表达式2,常量表达式3)  not  in  非数值的常量值,比如字符,要用单引号引起来。

      select 字段 from table where id in(55,66,77);  #查询id 为 55  66   77

      between  and  #判断某个字符的值是否介于xx之间。

      select 字段 from table where id between 30 and 90;  #

      =           #用于普通内容

      is null  is not  null   #用于null

       <=>        #安全等于,既能判断普通内容,又能判断null值

      举例:id<=> null;  id<=>22;

    5.排序查询

      

    select  查询列表
    from table
    where  筛选条件
    order by 排序列表

    select  * from table where id>100 order by salary asc;  #asc是升序,不写默认为升序。降序为desc。

    select *,num*12 总额 from table where id is not null order by 总额 desc;  #插入新算术表达式,并降序排列。

    按函数的结果排序:

      select 字段 from table order by lenth(字段);  #按字段字节长度升序排列  

      select 字段 from table order by char_lenth(字段);  #按字符长度排列

    select 字段1 字段2 字段3  
    from table 
    order by 字段1,字段2 desc;    #先按字段1升序排列,再满足按字段2降序排列。

    select * from table order by 字段;  #字段为第2列时,就写个   order by 2  也行。

    6.函数

    字符函数:拼接字符:concat

      select lenth('ab天');  #结果为5,一个汉字三个字节

      select char_lenth('ab天')  #结果为3。

    截取字符:SELECT SUBSTR('因为自己不够沙雕而感到自卑',7,2);  #7为起始索引(从1开始),2为长度,输出结果为 沙雕,不写长度截取到最后。

    获取字符出现索引:select instr('因为自己不够沙雕而感到自卑','自己');  #结果为3

    去空格:默认是去空格

    select trim('x' from 'xxxxxx本品xxxx' ) as  a ;

    SELECT TRIM( '   本品 ' ) AS  a ;  #结果都是下图

     填充:左填充 lpad  右填充rpad

     select lpad('木婉清',10,'a'); 

     

    select rpad('木婉清',4,'a');

    #列宽为1时,就一个木 字。

  • 相关阅读:
    Messy Code in Windows Server 2008 R2 English Edition
    Office Web Apps Server(1)
    How to add a webpart to your website
    How to Upload multiple files to documentLibrary in one time
    [刘阳Java]_SpringMVC文件上传第1季_第10讲
    [刘阳Java]_SpringMVC访问静态资源_第9讲
    react单组件 渲染页面
    react组件传值传方法
    react组件选项卡demo
    react组件
  • 原文地址:https://www.cnblogs.com/wddx5/p/12329003.html
Copyright © 2020-2023  润新知