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