从数据库取回数据的时候,需要限制所显示的数据行,或者将显示的结果行排序。
用查询限制行数(Limiting Rows Using a Selection)
用where子句限制输出行(Restrict the rows returned by using the WHERE clause.)
语法:
SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)];
限制选择的行
用WHERE子句限制从查询返回的行。一个WHERE子句包含一个必须满足的条件,WHERE子句紧跟着FROM子句。如果条件是true,返回满足条件的行。
在语法中:
WHERE 限制查询满足条件的行
condition 由列名、表达式、常数和比较操作组成
WHERE子句能够比较列值、文字值、算术表达式或者函数,WHERE子句由三个元素组成:列名,比较条件,列名、常量或值列表;
示例:
SELECT * FROM `dept` where deptno =10;
--字符串和日期,在WHERE子句中字符串和日期必须包含在单引号 (‘ ’) 中。但是,数字常数不应该包含在单引号中
SELECT * FROM `dept` where loc ='new YORK';
比较条件(Comparison Conditions):
Operator Meaning
= 等于
> 大于
>= 大于等于
< 小于
<= 小于等于
<> 不等于
BETWEEN...AND... 在两个值之间
IN(SET) 在一堆值里
LIKE 找一个想象的值
IS NULL 为一个NULL值
示例:
SELECT * FROM `dept` where deptno >=10;
SELECT * FROM `dept` where deptno BETWEEN 10 AND 30;
SELECT * FROM `dept` where deptno IN (10,30);
SELECT * FROM `dept` where loc LIKE 'n%';
SELECT * FROM `dept` where loc IS NULL;
逻辑条件(Logical Conditions):
Operator Meaning
AND 两个条件都为真,返回"true"
OR 其中一个条件为真,返回“true”
NOT 条件为假,返回"false"
示例:
SELECT * FROM `dept` where deptno>=10 AND loc LIKE "%n%"
SELECT * FROM `dept` where deptno>=10 OR loc LIKE "%n%"
SELECT * FROM `dept` where deptno NOT IN(10,20)
--优先原则(Rules of Precedence),从前往后:
SELECT * FROM `dept` where deptno NOT IN(10,20) AND deptno>30
排序(order by):
默认的是升序(ascending order, default)
示例:
SELECT * FROM `dept` order by deptno
--倒序
SELECT * FROM `dept` order by deptno DESC
--还可以根据多个条件进行排序
SELECT * FROM `dept` order by deptno,dname DESC
分享到:
相关推荐
oracle sql exam 1z0-061 practice exam chapter 3 - restricting & sorting data
2. **02 Restricting and Sorting Data.ppt**:在这个主题中,你会学习到如何使用WHERE子句来过滤查询结果,以及ORDER BY子句对查询结果进行排序,这些都是SQL查询中的核心部分。 3. **03 Single-Row Functions.ppt...
2. **Restricting and Sorting Data** - `WHERE`子句用于过滤数据,如`SELECT * FROM kong.authors WHERE au_id LIKE '1%'`。 - 数据类型如字符串和日期需要用单引号括起,且区分大小写。 - 比较运算符包括`=`, `...
1.2 Database, Database Server, and Database Language. . . . . . . . . 4 1.3 The Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 What Is SQL? . . . . . . . . . ...
总之,“Restricting Good Receipt Unless Production order confirmed”是一种定制化的SAP功能,它通过设置用户状态配置文件来限制未确认生产订单的收货操作,确保了生产流程的合规性和库存管理的有效性。...
Since this increase changes diffusion gradients between chamber air and soil air, a nonlinear increase is expected. Lateral gas flow and leakages also contribute to non linearity. Several models have...
sorting and grouping results; joining tables; managing users; other database elements such as triggers, stored procedures, functions, and events; and getting metadata information. Chapter 3, Using ...
See how programming paradigms impose discipline by restricting what developers can do Understand what’s critically important and what’s merely a “detail” Implement optimal, high-level structures...
5.2 Barring or Restricting the SMS-MO of One’s Own Subscribers 104 5.3 Intelligent Barring of SMS-MT 104 5.3.1 Origin Address-Based Barring 104 5.3.2 Filtering Based on Content of Incoming SMS-MT 105...
** A detailed guide on utilizing `grep`, a command-line utility for searching text patterns in files, including advanced usage and tips for filtering and sorting output. 3. **Manipulating Files with ...
提供的压缩包文件"Restricting-logon-to-SQL-Server.pdf"可能包含了更详细的教程和示例代码,"LoginTestExe.zip"和"LoginTestScript.zip"可能是用于测试和演示这些概念的应用程序和脚本,建议下载后仔细研究以加深...
Symantec, the Symantec Logo, and pcAnywhere are trademarks or registered trademarks of Symantec Corporation in the United States and certain other countries. Other names may be trademarks of their ...
This paper is a survey of the theory and methods of photogrammetric bundle adjustment, aimed at potential implementors in the computer vision community. Bundle adjustment is the problem of refining a ...
component separation, and data management, See how programming paradigms impose discipline by restricting what developers can do, Understand what’s critically important and what’s merely a “detail...
**Description:** The updated version of SQL*Plus includes enhancements such as support for large object (LOB) types and improved scripting capabilities. These improvements make SQL*Plus a more ...
and insufficiencies in the construction of big data information platforms restricting the development of intelligent transportation. These points are crucial for improving Zhengdong New Area's ...