Mysql的增删改查

 

一.增加数据(insert)

insert into 表名(列名1,列名2,列名3,.....列名n)values(值,值,值,....值)

insert into userinfo(id,name,age) values("0101","zhangsan","18")

增加数据还有一种写法为:

insert into 表名 set 列=值,set 列=值...set 列=值

#Integer类型,赋值的时候可以加引号也可以不加引号
insert into userinfo set id=0101,set name="zhangsan",set age=18

二.删除数据(delete)

delete from 表名

delete from userinfo

delete from 表名 where 列=值

delete from userinfo where id=13

三.更新数据(update)

update 表名 set 列=值,列=值,,,列=值 where 列=值

注意:这里只有一个set,update对应的set只有一个,逗号后边直接跟下一个要更新的属性。

update userinfo set username="1212",password="123" where username="zhangsan1"

四.查找数据(select)

普通查询

select 列 from 表名

select * from userinfo
#给列起别名
select id as userid from userinfo
#方法二
select id userid from userinfo

条件查询

select  列 from 表名 where 列=值

select id from userinfo where username="zhangsan"
select id from userinfo where username="zhangsan" and password="123"

排序order by

select 列 from 表名 where 列=值 order by 列

select * from userinfo where username="lisi" order by id

 模糊查询 like %/_

select 列 from 表名 where 列 like %zhang%

注:这里的 % 和通配符一样的意思,就是只要含有中间的因素就会查到。要加上双引号。

select * from userinfo where username like "zhang%"

分组 group by

select 列 from 表名 where 列=值 group by 列

select * 
from userinfo 
where username="lisi"
group by photo

查询前多少行 limit

select 列 from 表名 limit n

#查询前2行
select * 
from userinfo 
limit 2

#查询2之后的5行
select * 
from userinfo 
limit 2,5

去重 distinct

select distinct 列名1,列名2 …… from 表名;

注意:distinct后面的字段完全一致,才是重复的

select distinct username,password from userinfo

聚合查询

count() :记录查询列有多少行
SUM() :求数值序列的和
AVG() :求平均数
MAX() :求最大值
MIN() :求最小值

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值