Small problems with query result stitching in SQL Server

Source: Internet
Author: User

The day before yesterday's project, just took over, for the module is not very familiar with, there is a module, involving 4 tables, of which the main table there are two, as long as the clear on all return to the "peace".

Module requirements: The contents of the two tables to query out, the results are connected together. Master said finished, feel no problem, I looked at the database table design, after reading, found that the two tables of the primary key can be the same AH!

The project is not only designed to read, but also to read all the corresponding data according to the primary key, this look makes me feel very headache. Think of the previous teacher once said that in the last resort, do not use union or union all, which will greatly reduce performance.

So with the master to discuss can be replaced by a simple, separate to do, others Master said, separate can also, is the same business divided into two categories to do, a bad, two unattractive.

So Bai, go home to think method Bai, query simple, is how to distinguish the data from that table in the same case of the primary key problem.

I want to add a mutually exclusive column, try not to succeed,. Finally, a flag was added to distinguish the different tables.

The code is as follows:

SELECT a.biautoid, A.vcompanyname,'table_1' as from Capitalprojectkeytechnology   as aUNIONallSELECT b.biautoid, B.vcompanyname,'table_2'as from as   b

Small problems with query result stitching in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.