SQL回顾(二)

SQL where子句

①下面的 SQL 语句从 “Websites” 表中选取国家为 “USA” 的所有网站:

select * from websites where country ='USA';

在这里插入图片描述
SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。在上个实例中 ‘USA’ 文本字段使用了单引号。如果是数值字段,请不要使用引号。

②下面的 SQL 语句从 “Websites” 表中选取id为1或者id为5 的网站:

select * from websites where id=1 or id=5;

在这里插入图片描述

在这里插入图片描述
④下面的 SQL 语句从 “Websites” 表中选取alexa为1 的网站:

select * from websites where alexa=1;

在这里插入图片描述
⑤下面的 SQL 语句从 “Websites” 表中选取alexa>13 的网站:

select * from websites where alexa>13;

在这里插入图片描述
⑥下面的 SQL 语句从 “Websites” 表中选取alexa介于12~20范围内的网站:

select * from websites where alexa between 12 and 20;

在这里插入图片描述
⑦特殊条件
1.空值判断: is null
由于表中没有空地址的记录,因此我们插入一条url记录(注:此前设计表的时候url属性设置不允许为空,因此要先更改为允许为空,)

alter table websites modify url varchar(255) null;//设置url列允许为空

在这里插入图片描述

insert into websites values('6','aka',null,'32','JAPAN');//插入url为空的记录

在这里插入图片描述

查询url为空的记录

select * from websites where url is null;

在这里插入图片描述
2.In
查询websites表中alexa列值为1、13、32的值。

select * from websites where alexa in(1,13,32);

在这里插入图片描述
3.like模糊查询
查询url地址包含https的网站

select * from websites where url like  '%https%';

在这里插入图片描述
查询country列中倒数第二个字母为c的网站

select * from websites where country like  '%C_';

在这里插入图片描述
查询websites表country列中开头为C的网站:

select * from websites where country like 'C%';

在这里插入图片描述
总结:
% 表示多个字值,_ 下划线表示一个字符;
M% : 为能配符,正则表达式,表示的意思为模糊查询信息为 M 开头的。
%M% : 表示查询包含M的所有内容。
%M_ : 表示查询以M在倒数第二位的所有内容。

8.不带比较运算符的where子句:
WHERE 子句并不一定带比较运算符,当不带运算符时,会执行一个隐式转换。当 0 时转化为 false,1 转化为 true。例如:

select id from websites where 0;

在这里插入图片描述
则会返回一个空集,因为每一行记录where都返回 false。

select id from websites where 1;

在这里插入图片描述
返回websites表所有行中 id 列的值。因为每一行记录 where 都返回 true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值