0

I have T1 as table 1

----------------
field1 | field2
---------------
 val1  | val2

I have T2 as table 2

----------------
field1 | field2
---------------
 val11  | val22

I want to write a query which can produce result like this

 field1 | field2 | T1.T2 | 
 ------------------------|
 val11  | val22  | T2    | 
 val1   | val2   | T1    | 
3

Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC as

WITH CTE AS
(
  SELECT Col1, Col2, 'T1' [T1.T2]
  FROM T1
  UNION
  SELECT Col1, Col2, 'T2'
  FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;

Live Demo

-1

Maybe this query is useful... I used union to join the 2 tables.

mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
  • 1
    There is no MySQL tag there. – Sami Jan 18 at 7:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.