mysql常用语句总结

  1. 创建数据库:create database student
  2. 创建表:create table stu(id int,name varchar(20) not null,sex varchar(20) primary key(id))not null表明这个字段不为空。
  3. 插入数据:insert into stu (id,name,sex) values (1001,'ty','女')或者省略前面的如果每一个都一一对应的话像这样:insert into stu values (1001,'ty','女')
  4. 修改数据:修改所有sex为女:update stu set sex='女' 只更新ty的 :update stu set sex='女' where name='ty' 多列同时更新:update stu set sex='女',name='lm' where id=1001
  5. 删除数据:删除整张表:drop table stu 删除表中的数据:delete from stu 删除某条数据:delete from stu where id=1001
  6. 查询语句:查询表中所有数据: 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 
  7. 聚合函数: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
  8. 多表查询:内连接: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

消白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值