联合查询

想要查询同一数据库中两个不同的表,组合到一个结果集时,并且这两个表没有相等、包含等关系,可用联合查询,关键字UNION 

语法格式:

SELECT select_list 

FROM table_source

[WHERE search_conditions]

{UNION[ALL]

SELECT select_list 

FROM table_source

[WHERE search_conditions]

}

[ORDER BY order_expression]

实例如下:

<asp:SqlDataSource ID="SqlDataSource5" runat="server"  

        ConnectionString="<%$ ConnectionStrings:Production_ManagementConnectionString %>" 
 
        SelectCommand="SELECT GoodsNum AS 生产批号, FurnaceBanchNum AS 生产设备, Clean_ProControlType AS 清洗方式, Location_ProControlType AS 摆放方式, Proces_ProControlType AS 处理方式, gongyiNum AS 工艺编号, bianzhi_operator AS 编制人员, bianzhi_time AS 编制日期, shenhe_operator AS 审核人员, shenhe_time AS 审核日期 FROM DYL_GongYi WHERE (GoodsNum IN (SELECT ShengchanNum FROM Lailiao_Shengchan)) UNION SELECT GoodsNum AS 生产批号, FurnaceBanchNum AS 生产设备, Clean_ProControlType AS 清洗方式, Location_ProControlType AS 摆放方式, Proces_ProControlType AS 处理方式, gongyiNum AS 工艺编号, bianzhi_operator AS 编制人员, bianzhi_time AS 编制日期, shenhe_operator AS 审核人员, shenhe_time AS 审核日期 FROM ZKL_GongYi WHERE (GoodsNum IN (SELECT ShengchanNum FROM Lailiao_Shengchan AS Lailiao_Shengchan_1)) ORDER BY '生产批号' DESC">
       

    </asp:SqlDataSource> 

在查询分析器下运行sql语句的结果:

联合查询

你可能感兴趣的:(sql,联合查询)