zoukankan      html  css  js  c++  java
  • The difference between Union & Union All in SQL Server/pOSTGRESQL

    Following is test in SQL Server:

    USE [TestDB]
    CREATE TABLE [dbo].[UserInfoTest02](
        [number] [bigint] NOT NULL,
        [name] [varchar](20) NOT NULL,
        [age] [int] NOT NULL,
        [sex] [bit] NOT NULL,
        [address] [varchar](200) NULL
    )
    
    CREATE TABLE [dbo].[UserInfoTest03](
        [number] [bigint] NOT NULL,
        [name] [varchar](20) NOT NULL,
        [age] [int] NOT NULL,
        [sex] [bit] NOT NULL,
        [address] [varchar](200) NULL
    )
    
    CREATE TABLE [dbo].[UserInfoTest04](
        [SID] [bigint] NOT NULL,
        [name] [varchar](20) NOT NULL,
        [age] [int] NOT NULL,
        [sex] [bit] NOT NULL,
        [address] [varchar](200) NULL
    )
    
    Insert into UserInfoTest02
    (number,name,age,sex,address)
    Values
    (1, 'A',12,0,'A'),
    (2, 'B',12,0,'A')
    
    Insert into UserInfoTest03
    (number,name,age,sex,address)
    Values
    (1, 'A',12,0,'A'),
    (2, 'B',12,0,'BB')
    
    Insert into UserInfoTest04
    (SID,name,age,sex,address)
    Values
    (1, 'A',12,0,'A'),
    (2, 'B',12,0,'BB')
    
    
    SELECT SID, name,age,sex,address 
    FROM UserInfoTest04
    UNION ALL
    SELECT number,name,age,sex,address
    FROM UserInfoTest02

    --Test Result1

    SID    name    age    sex    address
    1    A    12    0    A
    2    B    12    0    BB
    1    A    12    0    A
    2    B    12    0    A
    SELECT SID, name,age,sex,address 
    FROM UserInfoTest04
    UNION
    SELECT number,name,age,sex,address
    FROM UserInfoTest02

    --Test Result2

    SID    name    age    sex    address
    1    A    12    0    A
    2    B    12    0    A
    2    B    12    0    BB

    SELECT number, name,age,sex,address 
    FROM UserInfoTest03
    UNION
    SELECT number,name,age,sex,address
    FROM UserInfoTest02

    --Test Result3

    number    name    age    sex    address
    1    A    12    0    A
    2    B    12    0    A
    2    B    12    0    BB

    Test in Postgresql:

    INSERT INTO "Test01"("SID", "Name") VALUES (1, 'A');
    INSERT INTO "Test01"("SID", "Name") VALUES (2, 'B');
    INSERT INTO "Test01"("SID", "Name") VALUES (3, 'C');
    
    INSERT INTO "Test02"("SID", "Name") VALUES (1, 'A');
    INSERT INTO "Test02"("SID", "Name") VALUES (2, 'B');
    
    INSERT INTO "Test03"("Number", "address") VALUES(1, 'A');
    INSERT INTO "Test03"("Number", "address") VALUES(2, 'B');
    SELECT "SID", "Name" FROM "Test01"
    UNION ALL
    SELECT "SID", "Name" FROM "Test02";

    --Test Result1:

    1;"A"
    2;"B"
    3;"C"
    1;"A"
    2;"B"

    SELECT "SID", "Name" FROM "Test01"
    UNION
    SELECT "Number", "address" FROM "Test03";

    --Test Result2:

    2;"B"
    1;"A"
    3;"C"

  • 相关阅读:
    [Windows Azure] Windows Azure Web Sites, Cloud Services, and VMs: When to use which?
    [Windows Azure] Windows Azure Execution Models
    [Windows Azure] Load Testing in Windows Azure
    [Windows Azure]The Autoscaling Application Block
    [Windows Azure]Windows Azure Identity
    [Windows Azure] Data Management and Business Analytics
    alienware Win8 系统安装
    Navisworks Addin 插件集成
    Navisworks 2014 Api 简单的使用
    Winform简单调用WebApi
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/4891602.html
Copyright © 2011-2022 走看看