美文网首页
数据库数据操作

数据库数据操作

作者: 寞离i | 来源:发表于2019-10-21 09:02 被阅读0次

    1.user表插入数据
    insert into user(id,name) values(1,'张三');
    insert into user values(1,'张三');
    注意:插入所有字段可不写字段,依次插入
    字符串必须用单引用
    2.user表删除数据
    delete from user where id=1;删除id为1的一行数据
    delete from user where name='张三';删除名字叫张三的所有数据
    delete from user;删除表中所有数据
    truncate from user;删除表中所有数据
    注意:虽然truncate和delete都可以删除表的所有记录,但有原理不同。delete的效率没有truncate高!
    truncate其实属性DDL语句,因为它是先drop table,再create table。而且truncate删除的记录是无法回滚的,但delete删除的记录是可以回滚的(回滚是事务的知识!)
    3.user表修改数据
    update user set name='张三' where id=1;将id为1的用户名字改成张三
    4.数据库查询语法
    SELECT selection_list 要查询的列名称
    FROM table_list 要查询的表名称
    WHERE condition 行条件
    GROUP BY grouping_columns 对结果分组
    HAVING condition 分组后的行条件
    ORDER BY sorting_columns 对结果排序
    LIMIT offset_start, row_count 结果限定
    5.运算符以及关键字
    条件查询就是在查询时给出WHERE子句,在WHERE子句中可以使用如下运算符及关键字:
     =、!=、<>、<、<=、>、>=;
     between…and;
     in(set);
     is null;
     and;
     or;
     not;

    6.查询user表性别为男的数据

    select * from user
    where sex='男';

    7.查询学号为1、2、3的人

    select * from user
    where id in(1,2,3);

    8.查询20岁以上的女生信息

    select * from user
    where sex='女' and age>20;

    9.查询学号为1或者张三的信息

    select * from user
    where id=1 or name='张三';

    10.查询学号不为1、2、3的学生的信息

    select * from user
    where id not in(1,2,3);

    11.查询名字为空的信息

    select * from user
    where name is null;

    12.查询名不为空的信息

    select * from user
    where name is not null;

    select * from user
    where not name is null;

    13.查询年龄在18-20岁的学生信息

    select * from user
    where age>18 and age <20;

    select * from user
    where age between 18 and 20;

    14.查询性别非男的信息

    select * from user
    where sex !='男';

    select * from user
    where sex <>'男';

    select * from user
    where not sex ='男';

    15.按年龄升序排序

    select * from user
    order by age(asc可不写,默认升序);

    16.按年龄降序排序

    select * from user
    order by age desc;

    17.分组查询每个班有几个人

    select * from user ,count(*)
    group by class;
    有几个班查询几条数据

    18.分组查询每个班有几个男生

    select * from user ,count(*)
    group by class
    having sex='男';

    19.分页查询

    select * from user
    limit 0,5;
    查询五条数据,起始行从0开始
    即第1,2,3,4,5条数据

    相关文章

      网友评论

          本文标题:数据库数据操作

          本文链接:https://www.haomeiwen.com/subject/qdmzmctx.html