zoukankan      html  css  js  c++  java
  • Mysql增删改查

    表:

    1.查看表创建语句:

    show create table 表名;

    2.删除表:

    drop table 表名;

    修改表结构:
    1.添加一个列:

    alter table 表名 add(列 列类型);

    2.修改列类型:

    alter table 表名 modify 列 列类型;

    3.修改列名;

    alter table 表名 change 原列名 新列名 列类型;

    4.删除列:

    alter table 表名 drop 列名;

    5.修改表名:

    alter table 原表名 rename to 新表名;

    6.查看指定的列
    select 列 from 表名;

    条件查询:

    条件:=,>,<,!=,<>,
    in(set),is null,and,or,not,between...and

    ----e查询性别是女,年龄大于50岁的(and)

    select * from stu where gender='female' and age>50;

    select sid,sname from stu where gender='female' and age>50;

    ------大于年龄50岁的学员信息(>)
    select * from stu where age>50;

    select * from stu where age=40;

    select * from stu where age< 30;

    select * from stu where sname<>'lisi';

    select * from stu where sname !='lisi';

    ----查询学号为1001 ,或者名字是李四(or)

    select * from stu where sid='S_1001' or sname='lisi';

    select sname,age from stu where sid='S_1001' or sname='lisi';

    ----查询学号在1001,1002,1003的学员信息

    select * from stu where sid in('S_1001','S_1002','S_1003');

    select age,gender from stu where sid in('S_1001','S_1002','S_1003');

    -----查询学号不在1001,1002,1003

    select * from stu where sid not in ('S_1001','S_1002','S_1003');

    select age,gender from stu where sid not in('S_1001','S_1002','S_1003');

    -----查询年龄为空的:
    select * from stu where age is null;

    insert into stu values('S_1012','aaa',null,'null');

    select * from stu where gender is null;

    select * from stu where gender ='null';

    ------查询年龄在20到40 (and)

    select * from stu where age>=20 and age<=40;

    select sid,sname from stu where age>=20 and age<=40;

    ------(between......and)

    select * from stu where age between 20 and 40;

    select sid,sname from stu where age between 20 and 40;

    ----查询性别非男学生信息

    select * from stu where gender !='male';

    select * from stu where gender <> 'male';

    或者:

    select * from stu where not gender='male';

    -----查询姓名不是空的

    select * from stu where not sname is null;

    insert into stu values('S_1013','null',12,'male');

    select * from stu where sname is not null;

    模糊查询
    -----模糊查询必须使用LIKE关键字。其中 “_”匹配任意一个字符,5个“_”表示5个任意字母。
    ----查询姓名有5个字母组成的

    select * from stu where sname like'_____';

    ----查询姓名有3个字母组成

    select * from stu where sname like'___';

    -----查询姓名有5个字母组成,并且第5个字母为i

    select * from stu where sname like'____i';

    ------查询姓名有五个字母组成,在第五个字母为i的学生ID,gender

    select sid,gender from stu where sname like'____i';

    ----查询姓名以z开头的学生信息
    ---%代表所有其中“%”匹配0~n个任何字母。(%表示任意个字符,从0到多个字符)

    select * from stu where sname like'z%';

    ----查询姓名以z开头有6字母组成

    select * from stu where sname like'z_____';

    -----查询姓名第二个字母为i的学生信息

    select * from stu where sname like '_i%';

    ----查询姓名中包含a学生信息

    select * from stu where sname like'%a%';

    字段控制查询

    select * from emp;

    ----去除重复记录:

    select distinct(去掉重复的) sal from emp;

    -----查看员工的月薪和奖金之和:

    select *(原来的所有的列),sal+ comm from emp;

    ---把奖金是为null的转换0(必须都是数值类型)
    select *,sal+ifnull(comm,0) from emp;

    ----给字段其别名:(关键字as 可以省略)

    select *,sal+ifnull(comm,0) as 总和 from emp;

    排序:(默认的是升序)
    ----order by
    ----asc 升序
    ----desc 降序

    select * from stu order by age;

    -年龄按照升序查询----

    select * from stu order by age asc;

    ----年龄按照降序排序
    select * from stu order by age desc;

    ----查询员工月薪按降序排序 ,如果月薪相同按编号排序

    select * from emp order by sal desc,empno asc;

    分组查询:group by

    limit
    ------查询前五行的信息
    select * from emp limit 0,5;
    -----查询10行记录从第三行开始:
    select * from emp limit 2,10;
    -----分页查询:
    ---如果一页记录为10条,希望查看第3页记录
    Select * from emp limit (3-1)*10,10

    个人笔记,部分转载,不喜勿喷。(如有侵犯隐私,请联系删除)
  • 相关阅读:
    第11组 团队Git现场编程实战
    团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    第11组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    JavaScript学习笔记----Window对象
    自学前端开发:模拟Array功能 不是扩展子类
  • 原文地址:https://www.cnblogs.com/colos/p/9961632.html
Copyright © 2011-2022 走看看