开发者

Are there alternatives to UNION for selecting the same columns from multiple tables?

开发者 https://www.devze.com 2023-04-10 05:33 出处:网络
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:

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 al开发者_运维知识库ternative 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).


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 :-)


No alternate for the UNION query developed by the database provider.


you can use table aliases. for instance:

select a.colA, b.colA, a.colB, b.colB
from table1 a
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号