zoukankan      html  css  js  c++  java
  • Mysql常用语句与函数(待续)

    -- 查询语句
    select class from stu_info where sid=1000000102;
    select * from stu_info t where t.age=88; -- t是表的别名,多表查询时比较方便
    select * from atable a, btable b where a.aID = b.bID;
    select * from stu_info t where t.age=99 or (t.age>20 and t.age <90);
    select * from stu_info t where t.age between 10 and 99;
    select * from stu_info t where t.hometown like '110%' ; -- %通配符,表示0到多个任意字符
    select * from stu_info t where t.hometown like '_10%' ; -- _通配符,表示任意一个字符
    select * from atable a where a.aID in (1,2);
    select * from atable a where a.aID not in (1,2);
    select * from atable a where a.aID in (select bID from btable);
    select * from atable order by aID desc -- asc升序|desc降序,默认为asc
    select name,count(*) as total from score_info group by name;
    select * from stu_info limit 10; -- 查询前十条,起始位置默认为0,且limit只能放到sql语句的最后
    select * from stu_info limit 101,110; -- 查询101-110条记录

    -- 更新语句
    update stu_info t set t.class='ceshi' where t.age=88;
    select * from stu_info t where t.class='ceshi';

    -- 插入语句
    insert into atable (aID,aNAME) values (8,'test'); -- 基本语法
    insert into atable values (6,'a20050111' ),( 7,'a20050112'); -- 不指定字段,则由左向右依次插入值
    insert into atable select * from btable; -- 前提是表结构一样或是按字段插入
    insert into atable select * from btable on duplicate key update anum=99;-- 如果存在主键冲突,则跳过主键执行update操作
    select * from atable;
    select * from btable;

    -- 删除语句
    test.rollback()
    delete from atable where aID=8; -- 条件删除,如果不加条件,则全表数据删除,可回滚
    truncate table atable; -- 删除全表数据,删除效率要高于delete,但是删除操作不可回滚
    drop table atable; -- 直接删表

    -- 修改表结构语句
    desc score_info; -- 查询表结构
    alter table score_info rename to score; -- 修改表名
    alter table score_info add sex char(2); -- 增加字段
    alter table score_info drop sex; -- 删除字段
    alter table score_info modify sex varchar(2); -- 修改字段类型,modify只可以修改字段类型
    alter table score_info change sex sosex char(2) -- 修改字段名称和字段类型

    -- 联表查询
    select * from a left join b on a.aID = b.bID; -- 左连接,查询出a表的全部记录,b表符合条件的记录
    select * from a right join b on a.aID = b.bID; -- 右连接,查询出b表的全部记录,a表符合条件的记录
    select * from a inner join b on a.aID = b.bID; -- 内连接或相等连接,取交集
    select * from a,b where a.aID = b.bID; -- 等价于内连接
    select * from a union select * from b; -- 查询a与b表的并集,结果去重
    select * from a union all select * from b;
    select * from b

    -- 常用函数
    select rand(); -- 该函数结果返回0-1之间的浮点型随机数,不可有参数
    select round(3.45); -- 该函数原型round(x,y),四舍五入方式返回最接近于参数x的数,其值保留到小数点后面y位,没有y时默认为0
    select round(3.45,1);
    select truncate(5.56,1); -- 函数原型truncate(x,y),参数一个不能少,返回x值保留y位小数
    select floor(9.88); -- floor(x)返回x的整数部分,采取直接截掉小数部分,同truncate
    select floor(rand()*1000);
    select curdate(); -- 返回当前日期,同current_date()
    select CURTIME(); -- 返回当前时间,同current_time()
    select now(); -- 返回当前日期时间
    select now()+0; -- +0后会去掉时间分隔符,返回一个时间数字串
    select date_format(now(),'%Y-%m-%d'); -- 格式化时间
    select ltrim(' apple') as ltrim; -- 去掉左边空格
    select rtrim(' apple ') as rtrim; -- 去右边空格
    select trim(' apple ') as trim; -- 去首尾空格

    sum(col);avg(col);count(col);min(col);max(col); -- 聚合函数常用到group by的select查询中

    select length('apple'); -- 返回字符创长度
    select concat(123,'apple',999); -- 拼接字符串
    select concat_ws('-',2015,07,08); -- 拼接字符串,并以'-'作为分割符
    select group_concat(anum) from a; -- 返回由一列值拼接组成的列表
    select left('apple',2); -- 返回最左边的2个字符;
    select right('apple',2); -- 返回最右边的2个字符
    select lower('APPLE'); -- 返回小写
    select upper('apple'); -- 返回大写
    select reverse('apple'); -- 返回倒序字符串

  • 相关阅读:
    SQL Server数据库中批量导入数据
    SQL里面也能用Split()
    MSSQL发送邮件
    Asp.Net简单的发邮件功能
    十大措施保证系统安全性
    WebDriver测试web中遇到的弹出框或不确定的页面
    WebDriver数据驱动模式
    nginx 优化(转)
    配置虚拟目录出错
    14152学期校内岗招聘信息
  • 原文地址:https://www.cnblogs.com/yinzx/p/4658541.html
Copyright © 2011-2022 走看看