zoukankan      html  css  js  c++  java
  • Sql Server UNION 组合查询

    --union 组合查询


    -- union 讲多条查询语句进行组合并汇合结果集 union默认去除重复的结果
    select cust_name,cust_contact,cust_email from Customers where cust_state in ('MI','OH','AZ')
    union
    select cust_name,cust_contact,cust_email from Customers where cust_name ='Fun4All';


    -- union all 查询全部结果 包括重复集 如果需要每个条件的匹配行全部出现, 那就不能使用where了 而是union all UNION ALL
    select cust_name,cust_contact,cust_email from Customers where cust_state in ('MI','OH','AZ')
    union all
    select cust_name,cust_contact,cust_email from Customers where cust_name ='Fun4All';

    -- 使用union进行组合查询时 使用order by 进行排序时 必须放在最后的一个查询语句里,并且只能对一个语句进行使用。
    --但order by 其实是对最终结果进行排序,而不是末尾查询语句进行排序。
    select cust_name,cust_contact,cust_email from Customers where cust_state in ('MI','OH','AZ')
    union
    select cust_name,cust_contact,cust_email from Customers where cust_name ='Fun4All' order by cust_name;


    --union 也可以不同的表进行组合查询
    select cust_name,cust_contact,cust_email from Customers where cust_state in ('MI','OH','AZ')
    union
    select prod_name ,prod_name ,prod_name from products;

    select prod_name ,prod_name ,prod_name from products;


    select * from abc


    create table abc( aid int)

    alter table abc add dda int --在已有表中新增列: alter table 表名 add 字段 字段类型

    insert into abc (id,name,aid) values (1,'aaa',3)

    insert into abc (id,name,aid) values (2,'bbb',2)

    insert into abc (id,name,aid) values (3,'ccc',2)

    insert into abc (id,name,aid) values (4,'ddd',1)

    select * from abc;

    select * from abc order by aid asc;

  • 相关阅读:
    简单的jQuery无缝向上滚动效果
    http://yuanma.wxb0.com/ 唯品源码网站
    vue.js 2.0 --- 安装node环境,webpack和脚手架
    记住密码后,密码框Password会自动带出数据
    http://ask.dcloud.net.cn/question/11695 nativeUI的使用(移动的)
    微信内置浏览器 如何小窗不全屏播放视频?
    webstrom快捷键
    6个html5页面适配iphone6的技巧
    rem的js
    docker打包容器成镜像文件、镜像文件加载到镜像
  • 原文地址:https://www.cnblogs.com/java-263/p/13593917.html
Copyright © 2011-2022 走看看