基本语法

‘*’ 表示通配符,也就是全部

注释

1
-- 注释

使用

1
use tableName

使用 tableName 表格

后续的操作都会基于 tableName 表格

查询

1
select * from tableName

查询 tableName 表格全部字段的数据


1
select field from tableName

查询 tableName 表格 field 字段的数据

查询多个字段使用 ‘,’ 隔开

插入

1
insert into tableName (field) values (value)

更新

1
update tableName set field=newValue where 条件

更新多个字段,需要使用 ‘,’ 隔开

删除

1
delete from tableName where 条件

限定语句

运算符

  • ‘=’: 等于
  • ‘!=’:不等于
  • ‘<>’:不等于
  • ‘<’:小于
  • ‘>’:大于
  • like %text%: 模糊查询
  • between number and number :在某个范围内
  • and:满足多个条件
  • or:满足任意一个条件

排序

1
2
3
4
5
-- 升序排序
select * from tableName order by field asc

-- 降序排序
select * from tableName order by field desc

默认升序排序

多重排序

在大范围排序的情况下,进行小范围的排序

1
select * from tableName order by field asc, field desc

总数

1
select count(*) from tableName where 条件

设置别名

为查询结果的字段取别名

1
select count(*) as alias from tableName where 条件

alias 指代别名