zoukankan      html  css  js  c++  java
  • SQL使用union合并查询结果(转载)

     UNION 指令的目的是将两个 SQL 语句的结果合并起来。从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料。 UNION 的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类。另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT)。 union只是将两个结果联结起来一起显示,并不是联结两个表………… UNION 的语法如下: [SQL 语句 1]
      假设我们有以下的两个表格,Store_Information 表格
        store_name  Sales  Date
      Los Angeles $1500 Jan-05-1999
      San Diego   $250   Jan-07-1999
      Los Angeles $300  Jan-08-1999
      Boston         $700  Jan-08-1999
     
      Internet Sales 表格
      Date              Sales
      Jan-07-1999 $250
      Jan-10-1999 $535
      Jan-11-1999 $320
      Jan-12-1999 $750
     
     而我们要找出来所有有营业额 (sales) 的日子。要达到这个目的,我们用以下的 SQL 语句:
      SELECT Date FROM Store_Information UNION SELECT Date FROM Internet_Sales 结果: Date
       Jan-05-1999
       Jan-07-1999
       Jan-08-1999
       Jan-10-1999
       Jan-11-1999
       Jan-12-1999
    有一点值得注意的是,如果我们在任何一个 SQL 语句 (或是两句都一起) 用 "SELECT DISTINCT Date" 的话,那我们会得到完全一样的结果。

    2. Union All
    UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALL 和 UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复。 UNION ALL 的语法如下: [SQL 语句 1]

    同样假设我们有以下两个表格, Store_Information 表格
        store_name  Sales  Date
      Los Angeles $1500 Jan-05-1999
      San Diego   $250   Jan-07-1999
      Los Angeles $300  Jan-08-1999
      Boston         $700  Jan-08-1999
     
      Internet Sales 表格
      Date              Sales
      Jan-07-1999 $250
      Jan-10-1999 $535
      Jan-11-1999 $320
      Jan-12-1999 $750
     
     而我们要找出有店面营业额以及网络营业额的日子。要达到这个目的,我们用以下的 SQL 语句:
      SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales 结果: Date
      Jan-05-1999
      Jan-07-1999
      Jan-08-1999
      Jan-08-1999
      Jan-07-1999
      Jan-10-1999
            Jan-11-1999
            Jan-12-1999

    3.更多sql示例
       --合并重复行
     select * from A
     union
     select * from B
     
     
     --不合并重复行
     select * from A
     union all
     select * from B
     
     
     按某个字段排序
     --合并重复行
     select *
     from (
     select * from A
     union
     select * from B) AS T
     order by 字段名
     
     --不合并重复行
     select *
     from (
     select * from A
     union all
     select * from B) AS T
     order by 字段名

      //sql server(WINDOWS平台上强大的数据库平台)版
      Select * From (
      select top 2 id,adddate,title,url from bArticle where ClassId='1' order by adddate desc) A
      Union All
      Select * From (
      select top 2 id,adddate,title,url from bArticle where ClassId='2' order by adddate desc) B
      Union All
      Select * From (
      select top 2 id,adddate,title,url from bArticle where ClassId='3' order by adddate desc) C
      Union All
      Select * From (
      select top 2 id,adddate,title,url from bArticle where ClassId='4' order by adddate desc) D
      
      
      //MySQL(和PHP搭配之最佳组合)版
      Select * From (
      select id,adddate,title,url from bArticle where ClassId='1' order by adddate desc limit 0,2) A
      Union All
      Select * From (
      select id,adddate,title,url from bArticle where ClassId='2' order by adddate desc limit 0,2) B
      Union All
      Select * From (
      select id,adddate,title,url from bArticle where ClassId='3' order by adddate desc limit 0,2) C
      Union All
      Select * From (
      select id,adddate,title,url from bArticle where ClassId='4' order by adddate desc limit 0,2) D

  • 相关阅读:
    PHP调用WCF提供的方法
    关于git报 warning: LF will be replaced by CRLF in README.md.的警告的解决办法
    vue中引入mui报Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them的错误
    微信小程序报Cannot read property 'setData' of undefined的错误
    Vue那些事儿之用visual stuido code编写vue报的错误Elements in iteration expect to have 'v-bind:key' directives.
    关于xampp中无法启动mysql,Attempting to start MySQL service...的解决办法!!
    PHP的环境搭建
    新手PHP连接MySQL数据库出问题(Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES))
    手机号码、获得当前时间,下拉框,填写限制
    团队作业(五):冲刺总结
  • 原文地址:https://www.cnblogs.com/haw2106/p/6483301.html
Copyright © 2011-2022 走看看