SQL基础查询语句

基础查询

select 查询列表 from 表名;

特点:

1、查询的结果集 是一个虚拟表

2、select类似于System.out.println();
select后面跟的查询列表,可以有多个部分组成,中间用逗号隔开
例如:select 字段1,字段2,表达式 from 表;
System.out.println();的打印内容,只能有一个

3、执行顺序
①from 子句
②select 子句

4、查询列表可以是:表达式、常量、字段、函数等

1、查询常量

`select 100;`

2、查询表达式

`select 100%3;`

3、查询单个字段

select 字段名 
from 表名;

4、查询多个字段

select 字段名,字段名 
from 表名;

5、查询所有字段

select * 
from 表名;

6、查询函数(调用函数、获取返回值)

select getdate();
select host_name();

7、起别名

方式1:使用as关键字

select 字段名 as 别名 from 表名;
select 字段名 as '别 名' from 表名;
select 字段名 as "别 名" from 表名;

方式2:使用空格

select 字段名 别名 from 表名;
select 字段名 '别 名' from 表名;
select 字段名 "别 名" from 表名;

8、 查询并拼接

select concat(字段名,字段名) 
as 别名 
from 表名;

9、distinct的使用(去除重复值)

select distinct 字段名 
from 表名;

10、isnull函数

isnull(表达式1,表达式2) mysql中为ifnull
表达式1:可能为空的字段或表达式
表达式2:如果表达式1为空,则最终将显示的值

select 表达式1,isnull(表达式1,'表达式1空,则显示表达式2')
as 别名 
from 表名;

条件查询

select 查询列表
from 表名
where 筛选条件;

执行顺序:
①from 子句
②where 子句
③select 子句

特点:

1.按条件表达式筛选:
关系运算符: > < >= <= = <> !=
补充:也可以使用!=,但不建议

2.按逻辑表达式:
逻辑运算符: and or not
补充: 也可以使用 && || ! ,但不建议

3.模糊查询:
like in between and ifnull

一、like(not like)

一般和通配符搭配使用,对字符型数据进行匹配查询

常见通配符:

_ 任意单个字符
% 任意多个字符

例:

select
from
where
like  '%a_';

二、in(not in)

功能:用于查询某字段的值是否属于指定的列表之内

a in(常量值1,常量值2,常量值3,…)

例:

select
from
where
in();

三、between and(not between and)

功能:判断某个字段的值是否介于xx之间

例:

select
from
where
between x and x;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值