赤峰北京网站建设免费网络推广软件有哪些
在 Navicat Premium 里面test_database数据库 ,右击 “命令列界面..”
命令列界面 中 输入命令
查看所有的数据库
show databases;
选择数据库
-- use 数据库名;
use test_database;
创建表
creat table 表名(字段名1 数据类型,字段名2 数据类型)
-- 创建一个user表,字段有id,name,age,gender
-- primary key设置主键 auto_increment 主键自增
-- not null 不能为空
-- default 设置默认值
create table user(id int primary key auto_increment,name varchar(10) not null,age int not null,gender char(1) default '女');
查看所有的表
show tables;
追加数据
insert into 表名(字段1, 字段2,字段3) values(属性值1,属性值2,属性值3);
-- 追加一条数据
insert into user(id, name, age, gender) values(null,"test1",30,default);
-- 追加多条数据
insert into user(id, name, age, gender) values
(null,"test1",30,default),
(null,"哈哈",30,default),
(null,"2test2",30,default),
(null,"1test2",30,default),
(null,"嘿嘿",32,default),
(null,"test2",30,default),
(null,"test3",32,default);
删除数据
delete from 表名 where 条件;
-- 删除user表中id 为2 的数据
delete from user where id=2;
修改数据
update 表名 set 字段="值";
-- 将 user 表中 id 为3的数据中 gender 改为 男
update user set gender="男" where id=3;
查询
查询user表中的所有字段
-- 查询user表中的所有字段
select * from user;
去重
distinct 去重
-- 去除user表中age年龄重复的数据
select distinct age from user;
查询user表5条数据
limit n 显示n条记录
-- 查询user表5条数据
select * from user limit 5;
查询user表age最大的年龄
max() 最大值
-- 查询user表age最大的年龄
select max(age) from user;
查询user表age最小的年龄
min() 最小值
-- 查询user表age最小的年龄
select min(age) from user;
查询user表age的平均年龄
as 取别名
avg() 平均数
-- 查询user表age的平均年龄
-- as 取别名 avg() 平均数
select avg(age) as 平均年龄 from user;
统计所有人数
count() 统计
-- 统计所有人数
select count(*) from user;
统计女生有多少个
count() 统计
-- 统计女生有多少个
select count(age) from user where gender="女";
根据id由高到低排序
order by 排序(asc 默认升序,desc降序)
-- 根据id由高到低排序
select * from user order by id desc;
like 模糊查找
like 模糊查找(“_”匹配一个,“%”匹配一个或多个)
-- 查询name 含有 test 的名字的数据
select * from user where name like"%test%";
-- 查询name 为test后有一个字符 的名字的数据
select * from user where name like"test_";
in 精确查找
in () 精确查找
-- 查询 gender 性别 为女
-- 方法1 用in() 精确查找
select * from user where gender in("女");
-- 方法2
select * from user where gender="女";
分组查询
group by 分组
-- 根据性别分组统计人数select gender,count(gender) from user group by gender;
查询的基础上筛选
having() 筛选
-- 根据年龄分组统计,筛选出age年龄大于31岁的人数
select age,count(*) as 总数 from user group by age having(age > 31);