Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL Tutorial - Merge Outputs from Two Queries

By: FYIcenter.com

(Continued from previous topic...)

How To Use UNION to Merge Outputs from Two Queries Together?

If you have two queries that returns the same row fields, you can merge their outputs together with the UNION operator. The following tutorial exercise shows you how to return all links that were created since year 2006 plus the one more link immediately before that:

mysql> SELECT id, url, created FROM fyi_links 
   ORDER BY created DESC;
+-----+-------------------+---------------------+
| id  | url               | created             |
+-----+-------------------+---------------------+
| 102 | dba.fyicenter.com | 2006-07-01 12:00:00 |
| 103 | sqa.fyicenter.com | 2006-07-01 12:00:00 |
| 101 | dev.fyicenter.com | 2006-04-30 00:00:00 |
| 104 | www.mysql.com     | 2006-01-01 00:00:00 |
| 105 | www.oracle.com    | 2005-01-01 00:00:00 |
| 106 | www.php.net       | 2004-01-01 00:00:00 |
| 107 | www.winrunner.com | 2003-01-01 00:00:00 |
+-----+-------------------+---------------------+
7 rows in set (0.00 sec)

mysql> SELECT id, url, created FROM fyi_links 
   WHERE created > '2005-12-31' ORDER BY created DESC;
+-----+-------------------+---------------------+
| id  | url               | created             |
+-----+-------------------+---------------------+
| 102 | dba.fyicenter.com | 2006-07-01 12:00:00 |
| 103 | sqa.fyicenter.com | 2006-07-01 12:00:00 |
| 101 | dev.fyicenter.com | 2006-04-30 00:00:00 |
| 104 | www.mysql.com     | 2006-01-01 00:00:00 |
+-----+-------------------+---------------------+
4 rows in set (0.00 sec)

mysql> SELECT id, url, created FROM fyi_links 
   WHERE created <= '2005-12-31' ORDER BY created DESC
   LIMIT 1;
+-----+----------------+---------------------+
| id  | url            | created             |
+-----+----------------+---------------------+
| 105 | www.oracle.com | 2005-01-01 00:00:00 |
+-----+----------------+---------------------+
1 row in set (0.01 sec)

mysql> (SELECT id, url, created FROM fyi_links 
   WHERE created > '2005-12-31' ORDER BY created DESC)
   UNION 
   (SELECT id, url, created FROM fyi_links 
   WHERE created <= '2005-12-31' ORDER BY created DESC
   LIMIT 1);
+-----+-------------------+---------------------+
| id  | url               | created             |
+-----+-------------------+---------------------+
| 101 | dev.fyicenter.com | 2006-04-30 00:00:00 |
| 102 | dba.fyicenter.com | 2006-07-01 12:00:00 |
| 103 | sqa.fyicenter.com | 2006-07-01 12:00:00 |
| 104 | www.mysql.com     | 2006-01-01 00:00:00 |
| 105 | www.oracle.com    | 2005-01-01 00:00:00 |
+-----+-------------------+---------------------+
5 rows in set (0.00 sec)

mysql> (SELECT id, url, created FROM fyi_links 
   WHERE created > '2005-12-31' ORDER BY created DESC)
   UNION 
   (SELECT id, url, created FROM fyi_links 
   WHERE created <= '2005-12-31' ORDER BY created DESC
   LIMIT 1) ORDER BY created DESC;
+-----+-------------------+---------------------+
| id  | url               | created             |
+-----+-------------------+---------------------+
| 102 | dba.fyicenter.com | 2006-07-01 12:00:00 |
| 103 | sqa.fyicenter.com | 2006-07-01 12:00:00 |
| 101 | dev.fyicenter.com | 2006-04-30 00:00:00 |
| 104 | www.mysql.com     | 2006-01-01 00:00:00 |
| 105 | www.oracle.com    | 2005-01-01 00:00:00 |
+-----+-------------------+---------------------+
5 rows in set (0.00 sec)

Note that the UNION operator damaged the sorting order of each subquery. You need a final "ORDER BY" clause at the UNION level to sort the final output.

  1. How To Join Two Tables in a Single Query?
  2. How To Write a Query with an Inner Join?
  3. How To Define and Use Table Alias Names?
  4. How To Write a Query with a Left Outer Join?
  5. How To Write a Query with a Right Outer Join?
  6. How To Write a Query with a Full Outer Join?
  7. How To Write an Inner Join with the WHERE Clause?
  8. How To Name Query Output Columns?
  9. What Is a Subquery?
  10. How To Use Subqueries with the IN Operator?
  11. How To Use Subqueries with the EXISTS Operator?
  12. How To Use Subqueries in the FROM clause?
  13. How To Count Groups Returned with the GROUP BY Clause?
  14. How To Return Top 5 Rows?
  15. How To Return the Second 5 Rows?
  16. How To Use UNION to Merge Outputs from Two Queries Together?

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...