如何在SQL中使用并集? [英] How to using union in SQL?

查看:244
本文介绍了如何在SQL中使用并集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我想在asp.net中通过查询获得名称为"Orders_T"和"Domain_T"数据的两个表,但是我的查询不起作用:

hello,
I want to get two table with name''s "Orders_T" and "Domain_T" data with a query in asp.net but my query dose not work:

SELECT * FROM dbo.Orders_T
UNION all
SELECT *
FROM   dbo.Domain_T


请帮帮我.
谢谢您的帮助.


Please help me.
thank you for your help.

推荐答案

请确保您要使用的2个表的列数和列名相同.您可能想要指定列名称,而不是像这样放置*.

Make sure the number of columns and the column names of your 2 tables are the same if you want it to work. You may want to specify the column names instead of putting *, like this.

SELECT fName, mName, lName FROM tblEmployeeInfo
UNION ALL
SELECT fName, mName, lName FROM tblStudentInfo


您需要了解实现UNION 条款的一些基础知识-请参见此处 [ ^ ].
You need to look at some of the basics of implementing a UNION clause - see here[^].


执行之前UNION操作必须知道这一点:

联盟规则

1.一个UNION必须由两个或多个SELECT语句组成,每个语句由关键字UNION分隔.
2.UNION中的每个查询必须包含相同的列,表达式或聚合函数,并且它们必须以相同的顺序列出.

3.Column数据类型必须兼容:它们不必完全相同,但必须是SQL Server可以隐式转换的类型.

希望对您有所帮助:)
Before performing UNION operation must know this:

UNION RULES

1.A UNION must be composed of two or more SELECT statements, each separated by the keyword UNION.
2.Each query in a UNION must contain the same columns, expressions, or aggregate functions, and they must be listed in the same order.

3.Column datatypes must be compatible: They need not be the same exact same type, but they must be of a type that SQL Server can implicitly convert.

hope it helps :)


这篇关于如何在SQL中使用并集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆