CRUD操作数据库

1.添加数据

insert into 表名 values("p007","李四",1,"n001","1988-2-3");

注意:如果该列类型为字符串,数据外添加单引号

           bool类型,添加0或1

           日期类型,格式为‘年-月-日’

            小数,整数类型,不加任何东西。

            表里有几列,添加的数据就有几列。

  只添加某几列的方法:insert into info(code,name,sex,nation) values(("p008","李四",1,"n001");

 2.修改数据

update 表名 set name='王五' where 条件

update info set name='王五' where code='p005'

update info set name='王五',sex=1 where code='p005'

3.删除数据:

delete from 表名 where 条件

delete from info where code='p008'

4.查询所有数据

select * from 表名

select * from info

查询指定列

select code,name form info

查询某些行的数据(条件查询)

select * from info where nation='n001'

3.给列指定名称

select code as '代号',name as '姓名' from info

4.条件查询

select * from info where code='p001'

select * from info where code='p001' or nation='n001' 或关系

select * from info where  code='p001' or nation='n001' 并且关系

5.模糊查询(经常用到)

select * from car where name like '%奥迪%'   前面后面有n个字符 

select * from car where name like '_奥迪%'     前面出现一个字符

6.排序查询

默认为主键排序

select * from car  order by price    以价格进行排序,默认按升序排序

select * from car order by price desc      按降序排列

select * from car order by price asc     按升序排列

select * from car order by price asc,oil desc 按两列排序

7.去重查询

select  brand from car 查询brand列

select distinct brand from car 去重查询

8.分页查询(取几条数据)

select * from car limit 5,5  跳过5条,取5条  (当前页-1)*5

9.统计查询(聚合函数)

  数据条数

 select count(*) from car  有多少数据

 select count(code) from car  只看主键列,可以提高查询效率

取最大值

select max(price) from car  取price列的最大值

最小值 

select min(price) from car 取price列的最小值

取平均值

select avg(price) from car 取price的平均值

10.分组查询

根据某一列分组,统计某一组的数量

select  brand, count(*) from car group by brand

select brand from car group by brand having count(*)>=3

11.范围查询

select * from car where price>=40 and price<=60

select * from car where price between 40 and 60

12.离散查询

select * from car where price in (10,20,30)

select * from car where price not in(10,20,30)

原文地址:https://www.cnblogs.com/niushuangmeng/p/8157623.html