mysql - Query Two Tables - but not trying to JOIN? -


i have 2 tables have identical columns. first table contains "current" state of particular record , second table contains previous stats of records (it's history table). second table has fk first table.

i'd query both tables entire records history, including current state in 1 result. don't think join i'm trying "joins" multiple tables "horizontally" (one or more columns of 1 table combined 1 or more columns of table produce result includes columns both tables). rather, i'm trying "join"(???) tables "vertically" (meaning, no columns getting added result, results both tables falling under same columns in result set).

not sure if i'm expressing make sense -- or if it's possible in mysql.

to accomplish this, use union between 2 select statements. suggest selecting derived table in following manner can sort columns in result set. suppose wanted combine results following 2 queries:

select fielda, fieldb table1; select fieldx, fieldy table2; 

we join these union statement follows:

select field1, field2 (     select fielda `field1`, fieldb `field2` table1      union select fieldx `field1`, fieldy `field2` table2)  `derived_table`  order field1 asc, field2 desc 

in example, have selected table1 , table2 fields similar, not identically named, sharing same data type. matched using aliases (e.g., fielda in table1 , fieldx in table2 both map field1 in result set, etc.).

if each table has same column names, field aliasing not required, , query becomes simpler.


note: in mysql necessary name derived tables, if name given not intended used.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -