zoukankan      html  css  js  c++  java
  • 学SQL(Select,order by,where(1))

    Select

    检索单独列

    Select prod_name
    from products;

    这是一个无无序的查询。

    注:SQL语句自动忽略“空白”,不区分大小写。

    检索多列

    Select prod_id,prod_name,prod_price
    from products;

    检索所有列

    Select * from products;

    Select Distinct

    Select vend_id
    from products;
    Select distinct vend_id
    from products;

    下面的语句只返回特定(唯一)的vend_id行,需要注意的是下面这种情况:

    select distinct vend_id,prod_price...

    将检索所有行,除非两个指定的行都是特定的,即distinct将应用于所有列。

    完全限定引用"."

    Select products.prod_name
    from crashcourse.products;

    prod_name来自products表;products来自crashcourse数据库。

    注释

    “--”放在一行开头表示注释。

    “/*”开始注释;“*/”结束注释。


    Order by

    --按照prod_name对检索结果以字母顺序排序(默认升序a-z)
    Select prod_name
    from products
    order by prod_name;

    注:也可按未检索列进行排序。

    --按多列排序(price由低到高,prod_name由a到z)
    Select prod_id,prod_name,prod_price
    from products
    order by prod_price,prod_name;

    多列检索时:只有当price列有重复时,按照name再排序,否则name排序无意义。

    --按序号排序:按照检索的第二列和第三列排序
    Select prod_id,prod_price,prod_name
    from products
    order by 2,3;
    --降序排列1
    Select prod_id,prod_price,prod_name
    from products
    order by prod_price DESC;
    --降序排列2
    Select prod_id,prod_price,prod_name
    from products
    order by prod_price DESC,prod_name;

    DESC关键词只对直接位于它们前面的列名起作用,若要在多列上降序,需要确保每个列都有自己的DESC关键字。

    注:在字典(dictionary)中,A等同于a。(管理员也可以更改这种规则)


    Where(1)

    where子句用于定义搜索条件(search criteria)/过滤条件(filter condition)。

    Select prod_name,prod_price
    from products
    where prod_price = 2.50;

    注:同时使用order by与where子句时,需要确保order by出现在where 子句后面。

    where子句的运算符

    运算符 描述
    = 相等性
    <>或!= 不等性
    < 小于
    <= 小于或等于
    > 大于
    >= 大于或等于
    between 在两个指定的值之间

    示例:

    --等于
    select prod_name,prod_price
    from products
    where prod_name='Fuses';
    select prod_name,prod_price
    from products
    where prod_name='fuses';/*结果与上一条select不同*/
    --小于
    select prod_name,prod_price
    from products
    where prod_price< 10;
    --小于或等于
    select prod_name,prod_price
    from products
    where prod_price<= 10;
    --不等于
    select vend_id,prod_name
    from products
    where vend_id<>1003;/*“<>”的含义是小于或者大于*/
    select vend_id,prod_name
    from products
    where vend_id!=1003;/*“!=”的含义是不等于*/
    --检查范围(值或日期)
    select prod_name,prod_price
    from products
    where prod_price between 5 and 10;
    --检查null
    select prod_name
    from products
    where prod_price is null;

    注:字符串数据类型需要使用引号,数值列不需要使用;

           由于未知(unknown)的特殊含义,在过滤匹配或不匹配的情况时不会返回具有NULL值的行。

    参考文献:《Oracle PL/SQL 必知必会》[美] Ben Forta 著 傅强 译 (第4章~第6章(31~63页))。

  • 相关阅读:
    learning scala view collection
    scala
    learning scala dependency injection
    learning scala implicit class
    learning scala type alise
    learning scala PartialFunction
    learning scala Function Recursive Tail Call
    learning scala Function Composition andThen
    System.Threading.Interlocked.CompareChange使用
    System.Threading.Monitor的使用
  • 原文地址:https://www.cnblogs.com/liuri/p/9363502.html
Copyright © 2011-2022 走看看