go

go


第三单元(条件查询)

<blockquote> <p>学习目标</p> </blockquote> <pre><code>能够写出查询的SQL语句</code></pre> <h2>1. where条件查询的介绍</h2> <pre><code>使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。 </code></pre> <hr /> <blockquote> <p>where语句支持的运算符:</p> </blockquote> <ul> <li>比较运算符</li> <li>逻辑运算符</li> <li>模糊查询</li> <li>范围查询</li> <li>空判断</li> </ul> <blockquote> <p>where条件查询语法格式如下:</p> </blockquote> <pre><code>select * from 表名 where 条件; 例: select * from students where id = 1;</code></pre> <h2>比较运算符查询</h2> <pre><code>等于: = 大于: &gt; 大于等于: &gt;= 小于: &lt; 小于等于: &lt;= 不等于: != 或 &lt;&gt; 例1:查询编号大于3的学生: select * from students where id &gt; 3; 例2:查询编号不大于4的学生: select * from students where id &lt;= 4; 例3:查询姓名不是“黄蓉”的学生: select * from students where name != '黄蓉'; 例4:查询没被删除的学生: select * from students where is_delete=0;</code></pre> <h2>3. 逻辑运算符查询</h2> <pre><code>and or not</code></pre> <pre><code>例1:查询编号大于3的女同学: select * from students where id &gt; 3 and gender=0; 例2:查询编号小于4或没被删除的学生: select * from students where id &lt; 4 or is_delete=0; 例3:查询年龄不在10岁到15岁之间的学生: select * from students where not (age &gt;= 10 and age &lt;= 15); 说明: 多个条件判断想要作为一个整体,可以结合‘()’。</code></pre> <h2>4. 模糊查询</h2> <pre><code>like是模糊查询关键字 %表示任意多个任意字符 _表示一个任意字符 例1:查询姓黄的学生: select * from students where name like '黄%'; 例2:查询姓黄并且“名”是一个字的学生: select * from students where name like '黄_'; 例3:查询姓黄或叫靖的学生: select * from students where name like '黄%' or name like '%靖';</code></pre> <h2>5. 范围查询</h2> <pre><code>between .. and .. 表示在一个连续的范围内查询 in 表示在一个非连续的范围内查询 例1:查询编号为3至8的学生: select * from students where id between 3 and 8; 例2:查询编号不是3至8的男生: select * from students where (not id between 3 and 8) and gender='男';</code></pre> <h2>6. 空判断查询</h2> <pre><code>判断为空使用: is null 判断非空使用: is not null 例1:查询没有填写身高的学生: select * from students where height is null; 注意: 不能使用 where height = null 判断为空 不能使用 where height != null 判断非空 null 不等于 '' 空字符串</code></pre> <blockquote> <p>小结</p> <ul> <li>常见的比较运算符有 &gt;,&lt;,&gt;=,&lt;=,!=</li> <li>逻辑运算符and表示多个条件同时成立则为真,or表示多个条件有一个成立则为真,not表示对条件取反</li> <li>like和%结合使用表示任意多个任意字符,like和_结合使用表示一个任意字符</li> <li>between-and限制连续性范围 in限制非连续性范围</li> <li>判断为空使用: is null</li> <li>判断非空使用: is not null</li> </ul> </blockquote>

页面列表

ITEM_HTML