SQL 常用基本语句总结大全—增删改查语句

 

SQL 语句的添加、删除、修改

 

添加、删除、修改使用 db.Execute(Sql)命令执行操作

 

() Select 语句

 

  1. 普通查询

 

  1. 查询全部的行和列

select * from users

 

  1. 查询部分的行并用 as 命名列(as 可用空格代替)

select UserName as 用户名,NickName 密码,sex from users where sex = 0 select UserName,NickName,sex from users where sex <> 0

  1. 查询空行
    1. select UserName from users where sex is null;
    2. select UserName from users where sex = "";
  2. 使用常量列

select UserName as  用户名,"中北大学" as "学校" from users;

  1. 限制固定行数

select UserId ,UserName as 用户名 from users limit 2,4;

  1. 升序排列 降序排列 按照多列排序
    1. select * from users where Height >178 order by age asc;
    2. select * from users where Height >178 order by age desc;
    3. select UserName as 用户名,Height  身高,Age as 年龄 from users where Age>20 order by Height asc, Age desc;

 

  1. 随机查询前 20 条数据

Select * from users order by rand () limit 20;

  1. 关键字顺序:

Where------group by-----having order by

 

  1. 高级查询:

 

  1. 模糊查询——like

select SName as 姓名 from Students where Sname like ‘张%’;

  1. 模糊查询——IS NULL

select Sname as 姓名,SAddress as 地址 form Students where SAddress is null

 

  1. 模糊查询——between

select StudentId,Score from Score where Score between 60 and 80

  1. 模糊查询——in

select Sname as 学员姓名,SAddress as 地址 from Students where SAddress in(‘北京’,’广州’,’上海’);

 

  1. 聚合函数

 

  1. 分组查询

 

分组查询——GROUP BY

 

    1.  

分组查询——多列分组

分组查询——HAVING

 

 

    1. 分组查询总结
      1. where 字句从数据源中去掉不符合其搜索条件的数据
      2. group by 字句搜集数据航到各个组中
      3. 统计函数为各个组计算统计值
      4. having 字句去掉不符合其搜索条件的各组数据行
      5. 使用 group by 时,select 后面出现的内容要么为聚合函数, 要么为 group by 后面出现的内容
      6. 关键字的先后顺序:where——group by——having——order by

 

 

多表连接查询

分类:

内连接(inner join):(等值连接、内连接)只返回两个表中连结字段相等的行外连接:

  1. 左外连接(left join)返回包括左表中的所有记录和右表中连结字段相等的记录
  2. 右连接(right join):返回包括右表中的所有记录和左表中连结字段相等的记录。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值