常用sql语句

来源:互联网 发布:单片机多路压力采集 编辑:程序博客网 时间:2024/06/06 01:01
  1. 插入单行
    insert into <表名> values (列值)
  2. 将现有数据添加到某个已有的表中
    insert into <被添加表名>(列,列,列) select 列,列,列 from <现有表名>
  3. 将现有数据填充一个新表
    select 列,列,列 into <新建表名> from <现有表名>
  4. 插入多行
    insert <表名>(列,列,列)
    select 列,列,列 union
    select 列,列,列

  1. 删除行
    delete from <表名> where 条件

  2. 删除表
    drop table tablename 删除内容及定义 ,释放空间
    truncate table tablename 删除内容,但不删除定义
    delete table where 条件 删除符合条件的内容


  1. 修改表
    update <表名> set 列名=更新值 where 条件

  1. 查询所有
    select * from 表名
  2. 部分查询
    select 列,列,列 from 表名 where 条件
  3. 精确查询
    select 列,列,列 from 表名 where 条件 order by 需排序的列名asc/desc
  4. 查询并更改列名
    select 列 as 新列名 from 表名 where 条件
  5. 查询空行
    select 列 from 表名 where 条件=null

    1. 查询中使用常量
      select 列, 变量名 as 新列名 from 表名 //查询并显示表列,并添加新列,值为“变量名”

    2. 限制返回数量
      select top num 列 from 表名 //显示前num行
      select top num percent 列 from 表名 //显示其num%


  1. like进行字符串的模糊查询
    select * from 表名 where 列名 like ‘张三’

  2. 使用between进行范围内的查询
    select * from 表名 where 列名 between num1 and num2

  3. 使用in进行枚举查询
    select 列 from 表名 where 列名 in (值,值,值)

  4. 使用group by进行分组筛选
    select 列 as 新列名, 列 as 新列 from 表名 group by 列

  5. 使用having进行分组筛选
    select 列 as 新列名, 列 as 新列 from 表名 group by 列 having 条件


  1. 查询多表
    select a.name,b.age from a,b where a.name=b.name

  2. 内联接查询
    select a.name,b.age from a inner join b on ( a.name=b.name)

  3. 外联接查询(左右)
    select a.name,b.age from a left outer join b on ( a.name=b.name)

select a.name,b.age from a right outer join b on ( a.name=b.name)