当前位置:网站首页>SQL基本查询
SQL基本查询
2022-06-11 08:43:00 【李泽信】
基础查询方式:
select 字段 from 表名;
select * from 表名;
去重
select distinct 字段 from 表名;
基础条件查询
select 字段 from 表名 where 条件 运算符 值;
| 运算符 | 名称 |
|---|---|
| = | 等于 |
| <> | 不等于 |
| > | 大于 |
| < | 小于 |
| >= | 大于等于 |
| <= | 小于等于 |
| BETWEEN | 在某个范围内 |
| LIKE | 包含 |
注:
在某些版本的sql中,操作符<>可以写为!=。
sql中使用单引号来环绕文本值(大部分数据库系统也可接受双引号),数值不用引号。
and和or运算符
and:如果第一个条件和第二个条件都成立,则and运算符显示一条记录。
or:如果第一个条件和第二个条件中只有一个成立,则or运算符显示一条记录。
select 字段 from 表名 where 字段 运算符 值 and/or 条件 运算符 值;
注:and与or结合(使用圆括号组成复杂表达式)
select 字段 from 表名 where (条件 运算符 值 and/or 条件 运算符 值) and/or 条件 运算符 值;
order by默认排序
desc 倒序
select 字段 from 表名 order by或desc;
插入
insert into 表名 values (值1,值2,...);
insert into 表名(字段1,字段2) voalues (值1,值2,...);
修改
update 表名 set 字段 = 新值 where 字段 = 某值;
删除
drop 删除表:删除内容和定义,释放空间
drop table 表名;
delete 删除内容,不删除表结构,一行一行删
delete table 表名;
delete from 表名 where 条件 = 值;
truncate 删除内容、释放空间,但不删除表结构;
truncate table 表名;
like模糊查询
select 字段 from 表名 where 字段 like 'N%';
注:“%”可用于第一通配符
通配符 描述
% 替代一个或多个字符
_ 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist]或者[!charlist] 不在字符列中的任何单一字符
in
select 字段 from 表名 where 字段 in (value1,value2,...);
between…and 选取介于两个值之间的数据范围,这些值可以是数值、文本或日期。
select 字段 from 表名 where 字段 between value1 and value2;
Alias别名
表的别名:
select 字段 from 表名 as 名称;
字段的别名:
select 字段 as 名称 from 表名;
join
join字段内容值一致inner join 内连接:产生的结果是两边的交集
select 字段 from 表名1 inner jion 表名2 on 表名1.字段=表名2.字段
left join 左连接:以左边的表为基准,不管是否满足条件都会把左边的数据全部展示,而右边的数据只显示能匹配上的,匹配不上时用null填充;
select 字段 from 表名1 left jion 表名2 on 表名1.字段=表名2.字段
right join 右连接:理解方式同左连接,只是换成了以右边的表为基准,左边的数据去匹配,只显示能匹配上的,匹配不上用null填充;
select 字段 from 表名1 right jion 表名2 on 表名1.字段=表名2.字段
FULL OUTER JOIN: 全连接,产生的结果是表A和表B的并集,也就是会全部显示出来
SELECT 字段
FROM 表1
FULL OUTER JOIN 表
ON 表1.字段=表2.字段;
Cross join:交叉连接
SELECT 字段 FROM 表1 CROSS JOIN 表2 WHERE 条件;
或
SELECT 字段 FROM 表1, 表2 WHERE 条件;
union内部的select 语句必须拥有相同数量的列。
列必须属于相同类型,同时列名顺序必须相同。
类型不同可强制转换cast (字段 as 类型:int float double varchar char等)
select 字段 from 表名1
union
select 字段 from 表名2
注:默认union操作符选取不同值,若允许重复的值,可使用union all。
select into 加入临时表
select into 语句从一个表中选取数据,然后把数据插入至另一个临时表中。
select * into 临时表 from old表;
select * into 临时表 from old表 where 条件 运算符 值;
select count()
count(*)函数返回表中的记录行数:
select count(*) from 表名;
select sum ()
sum 函数返回数之列的总数(总额)。
select sum(字段) from 表名;
注 :通常配合group by使用;
group by
group by 用于结合合计函数,根据一个或多个列对结果集进行分组;
select 字段1,sum(字段2) from 表名 group by 字段1;
边栏推荐
- js 中 Map 和 Set 的用法及区别
- Not eligible for getting processed by all beanpostprocessors
- Standardized compilation knowledge
- Hibernate L2 cache
- MATLAB R2022a 安装教程
- MySQL死锁问题如何解决?背诵版
- Web design and website planning assignment 13 making video playlists
- Type of SQL command (incomplete)
- EN45545-2 R26垂直燃烧测试介绍
- ActiveMQ simple tutorial, suitable for beginners, learning notes yyds
猜你喜欢

What is the process of en 1101 flammability test for curtains?

The interviewer asked four questions and summed up four experiences

Screaming Frog Log File Analyser 中文版安装教程

B+ super tree helps you know the underlying structure of MySQL

命名实体识别之CRF的实现方式

Matlab学习9-图像处理之非线性锐化滤波

File system check of the root filesystem failed

Hibernate L2 cache

Is the result too different from the goal? With the help of target management, you can reach the target accurately!

复制的代码格式混乱怎么办?
随机推荐
GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
Introduction to database system experiment report answer Experiment 5: database single table query
C语言打印菱形
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Installing MySQL and cluster operation on virtual machine in Linux system
SAP abap内表分类与增删改查操作
经典图论,深度优先和广度优先,拓扑,Prim和Krukal,该来温习啦
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗
PVC 塑料片BS 476-6 火焰传播性能测定
(一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)
[node] NPM part
BFS on tree (tree breathing first search)
Zipkin入门
[software tool] installation ffmpeg
马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup
深度学习入门之pytorch安装
Award winning survey | how Apache pulsar lived in 2022, you the final say
[software tool] the hacker matrix special effect software CMatrix
(二)从架构设计角度分析AAC源码-我的LiveData
EN 45545-2t10 precautions for smoke density detection by Horizontal method