找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 115|回复: 0

SQLServer基础语法大全

[复制链接] IP属地:北京市

39

主题

0

回帖

202

积分

管理员

积分
202
QQ
发表于 2024-7-17 11:45:53 | 显示全部楼层 |阅读模式
一、插入语句1. 普通插入

insert into tableA(字段1,字段2,字段3)values(字段值1,字段值2,字段值3)
例:

  1. insert into tableA(
  2.         userName,
  3.         userSex,
  4.         userPhone
  5. )values(
  6.         '张三',
  7.         '男',
  8.         '18812345678'
  9. )
复制代码
2. 表数据查询(复制)插入

insert into tableA(字段1,字段2,字段3)select (字段4,字段5,字段6) from tableB

  1. insert into tableA(
  2.         userName,
  3.         userSex,
  4.         userPhone
  5. )select
  6.         memberName,
  7.         memberSex,
  8.         memberPhone
  9. from tableB
  10. where ……
复制代码
二、查询语句1. 所有数据查询

select 字段1,字段2,字段3 from tableA where ……
例:

  1. select
  2.         userName,
  3.         userSex,
  4.         userPhone
  5. from tableA
  6. where ……
复制代码
2. 根据某条件查询前多少条数据

select top n 字段1,字段2,字段3 from tableA where ……
(n为数据条数)
例(前10条数据):

  1. select
  2.         top 10
  3.         userName,
  4.         userSex,
  5.         userPhone
  6. from tableA
  7. where ……
  8. order by userName
复制代码
三、更新语句1. 单表数据更新

update tableA set 字段1=‘字段值1’,字段2=‘字段值2’,字段3=‘字段值3’ where……
例:

  1. update tableA
  2. set userName='李四',
  3. userSex='男',
  4. userPhone='13012345678'
  5. where userName='张三'
复制代码
2. 多表联合数据更新

update a set a.字段1=b.字段4,a.字段2=b.字段5,a.字段3=b.字段6
from tableA a left join tableB b on a.userId=b.userId
where……
例:

  1. update a
  2. set a.userName=b.userName,
  3. a.userSex=b.userSex,
  4. a.userPhone=b.userPhone
  5. from tableA a
  6. left join tableB on a.userId=b.userId
  7. where ……
复制代码
四、删除语句

delete from tableA where ……
例:

  1. delete from tableA where userName='张三'
复制代码
五、case when……else

case when 条件1 then ……when 条件2 then……else……end
例:

  1. select
  2.         case when sexFlag=0 then '女'
  3.         when sexFlag=1 then '男'
  4.         else '未识别'
  5.         end userSex
  6. from tableA
复制代码
七、right join(右关联查询)

结果集返回右表(tableB)所有数据,若右表存在、左表(tableA)无数据则左表数据返回null
例:

  1. select
  2.         a.userName,--tableA表中人员姓名,若不存在则返回null
  3.         b.userOrderId--tableB中所有订单号
  4. from tableA a
  5. right join tableB b on a.userId=b.userId
复制代码
八、inner join(内关联查询)

结果集返回左表(tableA)和右表(tableB)都存在的数据
例:

  1. select
  2.         a.userName,--tableA表中人员姓名
  3.         b.userOrderId--tableB中所有订单号
  4. from tableA a
  5. inner join tableB b on a.userId=b.userId
复制代码
九、like

模糊条件查询,查询某个字段值包含某一字符串的结果集
例(查询姓名中含有【国】字的所有人员信息):

  1. select
  2. ……
  3. from tableA
  4. where userName like '%国%'
复制代码
十、concat

字符换拼接组合
例(姓名/性别/手机号组合为一个字段):

  1. select
  2. --返回:张三-男-18812345678
  3. CONCAT(userName,'-',userSex,'-',userPhone) as userInfo
  4. from tableA
  5. where ……
复制代码
十一、charindex

判断某字段值是否包含某一字符串,若包含则返回字符串的位置(大于0),若不存在则返回0,一般用于筛选条件
例(查询姓名中包含【国】字的所有人员信息):

  1. select
  2. ……
  3. from tableA
  4. where charindex('国',userName)>0
复制代码
十二、substring

截取字符串
例:

  1. select
  2. --手机号第4位开始截取,共截取4位
  3. substring(userPhone,4,4)
  4. from tableA
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表