0418数据库查询

测试1
create table ceshi1
{
Uid varchar(50) primary key,
pwd varchar(50),
Name varchar(50)
}
写查询语句需要注意:
1,创建表最后一行后面不加逗号
2 如果有多条语句一起执行,要在语句之间加分号
3 写代码符号都是英文的

关系型数据库:表和表之间是有关系存在的


创建表的几个关键字
1 主键 primary key
2 非空 not null
3 自增长列 auto_increment 整型才能增加
4 外键关系 foreign key(列名) references 表名(列名)

增删改查(CRUD)操作

1 添加数据
insert into info values('','','','','') 要求values括号里面的值得个数要和表里面列数相同
insert into info(code,name) values('','')添加指定列的值

2 修改数据
update info set name='zhangsan' where code='p001'

3 删除数据
delete from info where code='p001'

查询数据

1普通查询 查所有

select * from info 查所有数据
select code,name from info 查询指定列

2条件查询
select * from info where code='p001' 查询一个条件
select * from info where name='zhangsan' and nation='p001' 两个条件并列
select * from info where name='zhangsan' or nation='p001' 两个条件或的关系

3排序查询
select * from info order by birthday 默认升序排列asc 如果降序 desc
select * from 表 order by lie1,lie2 desc 多列排序

4 聚合函数

select count(*) from info 取个数
select sun(price) from car 查询price列的和
select avg(price) from car 查询price列的平均值
select max(price) from car 查询price列的最大值
select min(price) from car 查询price列的最小值


5 分页查询
select * from car limit n,m 跳过n条数据取m条数据

6 分组查询
select brand from car group by brand 简单分组查询
select brand from car group by brand having count(*)>2 查询系列里面车的数量大于2的

7 去重查询
select distinct brand from car

8 修改列名
select brand as ‘系列’from car

9 模糊查询
select * from car where name like '奥迪%' %代表任意多个字符 _代表一个字符

10 离散查询
select * from car where code in (‘c001’,‘c002’,‘c003’,‘c004’)
select * from car where code notin (‘c001’,‘c002’,‘c003’,‘c004’)

原文地址:https://www.cnblogs.com/wcc731546227/p/5404713.html