是否有替代 UNION 的方法来从多个表中选择相同的列?
我有 4-5 个具有几乎相同名称的表,我需要从中选择具有相同名称的列。到目前为止,我正在这样做:
SELECT colA, colB FROM Table1
UNION
SELECT colA, colB FROM Table2
UNION
etc...
如果您有多个类似的表并且想知道是否有任何替代语法,那么它似乎可能过于冗长。理想情况下,我想要一些通配符,例如
SELECT colA, colB FROM Table%
如果没有 UNION
的替代品,为什么不呢?是否有不允许这样做的具体原因?我想这是因为 SQL 在表定义(表 + 列名称、类型等)方面应该尽可能具体,同时允许数据操作的灵活性(因此我们对行使用通配符)。
I've got 4-5 tables that have near identical names and I need to select columns which have the same names from them. So far I'm doing this:
SELECT colA, colB FROM Table1
UNION
SELECT colA, colB FROM Table2
UNION
etc...
It seems like it could be overly verbose if you had more than a handful of similar tables and was wondering if there's any alternative syntax. Ideally I'd like something wildcard-y, like
SELECT colA, colB FROM Table%
If there is no alternative to UNION
, why not? Is there a specific reason for not allowing this? I would imagine it's because SQL should be as specific as possible when it comes to the table definitions (table + col names, types, etc) while allowing flexibility in data manipulation (hence we have wildcards for the rows).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我别无选择。如果我很挑剔,我会说如果您有包含类似数据的类似表,您应该重新考虑您的数据库设计:-)
There's no alternative I could think of. If I were nitpicky, I'd say that if you have similar tables that contain similar data, you should reconsider your database design :-)
数据库提供商开发的 UNION 查询没有替代方案。
No alternate for the UNION query developed by the database provider.
您可以使用表别名。
例如:
you can use table aliases.
for instance: