zoukankan      html  css  js  c++  java
  • sql语句查询结果合并union all用法

    整理别人的sql

    大概的思想是用union 和union all

    --合并重复行
    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版
    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版
    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

  • 相关阅读:
    tomcat 支持https
    linux环境下jdk 安装以及maven私服搭建
    消息中间间初识
    HDU 5527 Too Rich
    HDU 5534 Partial Tree
    HDU 5543 Pick The Sticks
    HDU 5542 The Battle of Chibi dp+树状数组
    CodeForces 842D Vitya and Strange Lesson
    Codeforces 858D Polycarp's phone book
    HDU 5489 Removed Interval
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/10281286.html
Copyright © 2011-2022 走看看