`

SQL之限定和排序(Restricting and Sorting)

    博客分类:
  • sql
SQL 
阅读更多

从数据库取回数据的时候,需要限制所显示的数据行,或者将显示的结果行排序。

用查询限制行数(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
分享到:
评论

相关推荐

    OCA Oracle Database 11g SQL Fundamentals I Exam Guide (Exam 1Z0-051).pdf

    3 Restricting and Sorting Data 4 Single-Row Functions 5 Using Conversion Functions and Conditional Expressions 6 Reporting Aggregated Data Using the Group Functions 7 Displaying Data from Multiple ...

    oracle 061

    oracle sql exam 1z0-061 practice exam chapter 3 - restricting & sorting data

    Sql for mysql

    1.2 Database, Database Server, and Database Language. . . . . . . . . 4 1.3 The Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 What Is SQL? . . . . . . . . . ...

    Architecture and Design for the Future Internet:4WARD Project

    研究关于未来互联网的架构和设计的相关问题。 Architecture and Design for the Future Internet addresses the Networks of the Future and the Future Internet, focusing on networks aspects, offering both ...

    Restricting Good Receipt Unless Production order confirmed

    Restricting Good Receipt Unless Production order confirmed 当生产订单确认时才允许收货

    Your Excel Survival Kit: Your Guide to Surviving and Thriving in an Excel world

    Sorting and Filtering Summary Chapter 3 Further Cleaning,Slicing,and Dicing Removing/Completing Blank Columns/Rows/Cells Cleaning,Combining,and Amending Existing Data Extracting Specific Pieces of ...

    mysql-8-cookbook2018

    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 ...

    Restricting the nonlinearity parameter in soil greenhouse gas flux calculation

    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...

    Clean Architecture A Craftsman's Guide to Software Structure and Design

    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...

    Swift.Pocket.Reference.Programming.for.iOS.and.OS.X.2nd.Edition.149194

    Access control: restricting access to types, methods, and properties Ranges, intervals, and strides A full list of built-in global functions and their parameter requirements Table of Contents ...

    Joomla! 1.5 Development Cookbook.pdf

    Restricting a user's access using Public, Registered, and Special 111 Getting the user's parameters 113 Setting the user's parameters 115 Extending and editing user parameters 116 Sending an email...

    Artech House - SMS and MMS Interworking in Mobile Networks

    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...

    整洁架构:软件结构设计

    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 ...

    安装制作软件

    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 ...

    Bundle Adjustment —A Modern Synthesis

    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 ...

    高速串行接口协议Interlaken_Protocol_Definition

    interface it lacks channelization and flow control, restricting it from several applications. And both protocols offer only fixed configurations, limiting the ability of the designer to tailor the ...

    Clean Architecture

    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...

    Hardening.Azure.Applications.1484209230

    This 208 page book covers the techniques and engineering principles that every architect and developer needs to know to harden their Azure/.NET applications to ensure maximum reliability and high ...

    Vulkan CookBook

    Computer graphics have a very long and interesting history. Many APIs or custom approaches to the generation of 2D or 3D images have come and gone. A landmark in this history was the invention of ...

Global site tag (gtag.js) - Google Analytics