MySQL基础命令

147 阅读1分钟
查询语句
  • 查询所有字段:select * form 表名
  • 查询name字段:select name from 表名
  • 查询name和age字段:select name,age from 表名
删除语句

从表中删除name为xxx的记录:delete from 表名 where name='xxx'

插入语句

insert into 表名 (name,age,phone) values('xxx',50,'18032485454')

修改语句

update 表名 set name='veb',age=50 where Id=2

where子语句
  • 条件查询: select * from 表名 where Id=9
  • 指定范围: select name,age from 表名 where age>=25
  • 同时满足两个条件: select name,age from 表名 where phone='1506065500' and age=25
  • 满足任意一个条件: select name,age from 表名 where phone='1506065500' or age=25
like子语句, 模糊查询
  • 以a结尾的数据: '%a'
  • 以a开头的数据: 'a%'
  • 含有a的数据: '%a%'
  • 三位且中间字母是a的: '_a_'
  • 两位且结尾字母是a的: '_a'
  • 两位且开头字母是a的: 'a_'

查询name字段以李开头的记录: select * from 表名 where name like '李%'

limit子语句(用于分页查询)

从第几条开始 查询几条内容: select * from 表名 limit 0,3

按Id的顺序查询

ASC 是正序排 也可以修改成 DESC 是倒序排: select * from A order by Id ASC

注意如果需要排序并分页, 应该先排序再分页: select * from goods order by Id DESC limit 0,5