zoukankan      html  css  js  c++  java
  • Mysql | 总结 | 常用的查询语句(单表查询)

    1. 查询单表全部

      select* from 数据表名;

    2. 查询单表中一个或者多个字段

      select 字段1,字段2 from 数据表名;

    3. 查询单表中的指定信息

      select* from 数据表名 where 属性= .....;

     

      查询所有红色的产品号

    SELECT ProductNumber
    from SalesLT.Product
    WHERE Color = 'Red'

      

    4. 带有 IN 关键字的查询

       Select* from 数据表 where 字段 [NOT] IN (字段值1,字段值2…字段值n);   

                                <注:最适于数组作为查询条件,即:in (数组数据)>

    5.带BETWEEN and 的范围查询

      select * from 数据表 where 字段 [NOT] BETWEEN 取值1 and 取值2;

    6. 带like的字符匹配查询

      select * from 数据表 where 属性 like '%SQL%';//查询属性中包含SQL字符的数据

     

    ---查询产品名称中包含'Road'关键字的。

    SELECT Name
    FROM SalesLT.Product
    WHERE Name like '%Road%'
    GO

      

      select * from 数据表 where 属性 like 'a%b';// //查询属性中以a开头以b结尾的字符串的数据

      select * from 数据表 where 属性 like 'm_n';//查询属性中以m开头以n结尾的3个字符的数据,中间的‘_’只能代表一个字符

    7.带AND的多条件查询

       select* from 数据表名 where 属性= ..... AND 属性=......;

      

      --查询成本价介于200到300之间的产品名称和成本价。

    SELECT Name,StandardCost
    FROM SalesLT.Product
    WHERE StandardCost >= 200 and StandardCost <= 300
    GO

      

    8. 带OR的多条件查询

      select* from 数据表名 where 属性= ..... OR 属性=......;

     --查询LastName是Li、Liu、Lang的客户姓名

    SELECT concat(FirstName ,' ', LastName)as Name
    FROM SalesLT.Customer
    WHERE LastName = 'Li' or LastName = 'Liu' or LastName = 'Lang' 
    GO

      

    9.用DISTINCT关键字去除结果中的重复行

      查询Address表中,不同的国家和省份。
    Select  DISTINCT  CountryRegion,StateProvince
    from SalesLT.Address
    GO

      

    10. 用ORDER BY 关键字对查询结果排序

       查询所有产品,包括产品名称、销售价、成本价和利润(销售价与成本价之差),并按利润从高到低的顺序排序

    select Name,ListPrice,StandardCost,ListPrice-StandardCost as profit
    from SalesLT.Product order by profit desc

      

    11.  用GROUP BY关键字分组查询

      (1)    用GROUP BY关键字分组查询

      (2)    GROUP BY 关键字与 GROU_CONCAT函数一起使用

      (3)按多个字段进行分组

    12. 用LIMIT限制查询结果的数量

    样例:

    --1.查询所有客户,将Title、FirstName和LastName合并成一列,之间用空格分开
    SELECT CONCAT(Title,' ',FirstName,' ',LastName) AS name
    FROM SalesLT.Customer
    GO

     --5. 查询销售价格小于100的产品号和销售价格

    SELECT  ProductNumber,ListPrice
    from SalesLT.Product
    where ListPrice < 100
    GO

      

    --9. 查询已停止销售(SellEndDate不为空)的产品名称,以及产品的销售天数。 函数处理

    SELECT Name,DATEDIFF(day,SellStartDate,SellEndDate)  as Day
    FROM SalesLT.Product 
    WHERE SellEndDate is not null
    GO

      

    -- 10. 查询客户所在公司名称中包含bike或bicycle的用户名和公司名。

    SELECT concat(FirstName ,' ', LastName)as Name,CompanyName
    FROM SalesLT.Customer
    WHERE CompanyName like '%bike%' or CompanyName like '%bicycle%'
    GO

      

    --11. 查询分类号(ProductCategaryID)是18,红色的产品编号,并按尺寸从小到大的顺序排列

    SELECT ProductNumber
    from SalesLT.Product
    WHERE Color = 'Red' and ProductCategoryID = 18
    order by Size 
    GO

      

    --12. 汇总所有订单的小计值、税款、运费和总金额。 Sum求和

    SELECT SUM(SubTotal ) as SubTotal ,SUM(TaxAmt)as TaxAmt,
            SUM(Freight) as Freight,SUM(TotalDue) as TatalDue
    from SalesLT.SalesOrderHeader
    go

      

     --13. 查询累计订单总金额超过10万的客户号和总金额。

    SELECT CustomerID ,SUM(SubTotal) as SubTotal 
    from SalesLT.SalesOrderHeader 
    where SubTotal>100000
    group by CustomerID
    go

      

  • 相关阅读:
    Java高级之类结构的认识
    14.8.9 Clustered and Secondary Indexes
    14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器
    14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
    14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
    14.8.1 Creating InnoDB Tables 创建InnoDB 表
    14.7.4 InnoDB File-Per-Table Tablespaces
    14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
    14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小
    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB
  • 原文地址:https://www.cnblogs.com/jj81/p/9847630.html
Copyright © 2011-2022 走看看