- 创建数据库:create database student
- 创建表:create table stu(id int,name varchar(20) not null,sex varchar(20) primary key(id))not null表明这个字段不为空。
- 插入数据:insert into stu (id,name,sex) values (1001,'ty','女')或者省略前面的如果每一个都一一对应的话像这样:insert into stu values (1001,'ty','女')
- 修改数据:修改所有sex为女:update stu set sex='女' 只更新ty的 :update stu set sex='女' where name='ty' 多列同时更新:update stu set sex='女',name='lm' where id=1001
- 删除数据:删除整张表:drop table stu 删除表中的数据:delete from stu 删除某条数据:delete from stu where id=1001
- 查询语句:查询表中所有数据: select * from stu 查询某一列数据:select name from stu 查询某几列数据:select id,name from stu 起别名(查询后列名会改变):select name as "名字" from stu 从查询数据中去除重复:select distinct sex from stu 从查询数据中去除多行数据:select distinct sex,name from stu 条件查询:select sex from stu where name='ty' 添加一个属性年龄,并将年龄减半:select age/2 from stu 模糊查询:查询姓名包含林的数据:select name from stu where name like"%林%" (如果前面包含在前面加%,如果后面包含在后面加%)查询多条数据:select name from stu where id in(1001,1002,1003) 查询年龄大于10小于20:select name from stu where age>10 and age<20 查询年龄大于10或小于20:select name from stu where age>10 or age<20
- 聚合函数:select count(name) from stu 计算所有数据数量:select count(*) from stu 计算年龄的最大值、最小值、平均值、和: select max(min,avg,sum)(age) from stu 聚合函数去除重复值:select count(distinct name) from stu 分组:select age,count(*) from stu group by age 对分组过滤:select age,count(*) from stu group by age having count(*)=2 按照年龄进行升序排列:select * from stu older by age(默认升序)先根据年龄,再根据名字排序:select * from stu older by age,name 按照年龄进行降序排列:select * from stu older by age desc 先根据年龄降序,再根据名字升序:select * from stu older by age desc,name asc
- 多表查询:内连接:select * from stu s inner join teacher t on s.id=t.id 左连接:select * from stu s left join teacher t on s.id=t.id 右连接:select * from stu s right join teacher t on s.id=t.id
mysql常用语句总结
最新推荐文章于 2022-06-15 11:26:50 发布