Home >> FAQs/Tutorials >> MySQL Tutorials >> Index

MySQL FAQs - SQL SELECT Statements with JOIN and Subqueries

By: FYIcenter.com

Part:   1  2  3  4  5  

(Continued from previous part...)

How To Return the Second 5 Rows?

If you want to display query output in multiple pages with 5 rows per page, and the visitor wants to see the output for the second page, you need to display query output from row 6 to row 10. You can use the "LIMIT startRow maxRows" clause to return only rows starting from "startRow" to "startRow+maxRows-1". Note that MySQL counts output rows starting with 0.

The following statement returns the second 5 rows first from the fyi_links table:

mysql> SELECT id, url, counts, tag 
   FROM fyi_links ORDER BY counts DESC LIMIT 5, 5;
   -- the first "5" means starting with row #6.
+-----+-------------------+--------+------+
| id  | url               | counts | tag  |
+-----+-------------------+--------+------+
| 102 | dba.fyicenter.com |      3 | DBA  |
| 104 | www.mysql.com     |      1 | DBA  |
+-----+-------------------+--------+------+
2 rows in set (0.00 sec)

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.

Part:   1  2  3  4  5  


Selected Developer Jobs:

More...