Pulling a Section from the Middle of a Result Set : Limits « Select Clause « SQL / MySQL






Pulling a Section from the Middle of a Result Set

      
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  id              INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name    CHAR(20) NOT NULL,
    ->  birth   DATE,
    ->  color   ENUM('blue','red','green','brown','black','white'),
    ->  foods   SET('lutefisk','burrito','curry','eggroll','fadge','pizza'),
    ->  cats    INT,
    ->  PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytable
    ->  VALUES
    ->          (NULL,'Jack','1970-04-13','black','eggroll,pizza,fadge',0),
    ->          (NULL,'Tom','1969-09-30','white','curry,eggroll,burrito',3),
    ->          (NULL,'Mary','1957-12-01','red','burrito,pizza,curry',1),
    ->          (NULL,'Jane','1973-11-02','red','pizza,eggroll',4),
    ->          (NULL,'Sean','1963-07-04','blue','burrito,curry',5),
    ->          (NULL,'Alan','1965-02-14','red',',curry,eggroll',1),
    ->          (NULL,'March','1968-09-17','green','fadge,lutefisk',1),
    ->          (NULL,'Shane','1975-09-02','black','pizza,curry',2),
    ->          (NULL,'Dan','1952-08-20','green','fadge,lutefisk',0),
    ->          (NULL,'Tony','1960-05-01','white','pizza,burrito',0)
    -> ;
Query OK, 10 rows affected, 1 warning (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 1

mysql>
mysql> SELECT * FROM mytable;
+----+-------+------------+-------+-----------------------+------+
| id | name  | birth      | color | foods                 | cats |
+----+-------+------------+-------+-----------------------+------+
|  1 | Jack  | 1970-04-13 | black | eggroll,fadge,pizza   |    0 |
|  2 | Tom   | 1969-09-30 | white | burrito,curry,eggroll |    3 |
|  3 | Mary  | 1957-12-01 | red   | burrito,curry,pizza   |    1 |
|  4 | Jane  | 1973-11-02 | red   | eggroll,pizza         |    4 |
|  5 | Sean  | 1963-07-04 | blue  | burrito,curry         |    5 |
|  6 | Alan  | 1965-02-14 | red   | curry,eggroll         |    1 |
|  7 | March | 1968-09-17 | green | lutefisk,fadge        |    1 |
|  8 | Shane | 1975-09-02 | black | curry,pizza           |    2 |
|  9 | Dan   | 1952-08-20 | green | lutefisk,fadge        |    0 |
| 10 | Tony  | 1960-05-01 | white | burrito,pizza         |    0 |
+----+-------+------------+-------+-----------------------+------+
10 rows in set (0.00 sec)

mysql>
mysql> SELECT * FROM mytable ORDER BY birth LIMIT 2,1;
+----+------+------------+-------+---------------+------+
| id | name | birth      | color | foods         | cats |
+----+------+------------+-------+---------------+------+
| 10 | Tony | 1960-05-01 | white | burrito,pizza |    0 |
+----+------+------------+-------+---------------+------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT * FROM mytable ORDER BY birth DESC LIMIT 2,1;
+----+------+------------+-------+---------------------+------+
| id | name | birth      | color | foods               | cats |
+----+------+------------+-------+---------------------+------+
|  1 | Jack | 1970-04-13 | black | eggroll,fadge,pizza |    0 |
+----+------+------------+-------+---------------------+------+
1 row in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Limit the query results: Get 3 Most Expensive Products
2.Use LIMIT in SELECT
3.Use LIMIT
4.Another ORDER BY and limit
5.Simple LIMIT
6.Retrieving the Top Five Students
7.ORDER and limit
8.Limits and order
9.Scrolling through the entire product table four records at a time
10.Determining the Number of Records Suppressed by LIMIT (SQL_CALC_FOUND_ROWS, FOUND_ROWS)
11.Limiting a Selection Using LIMIT
12.Use ORDER BY to sort the result set. Then you can use LIMIT to find smallest and largest values.
13.Order the rows with the largest SUM( ) values first and use LIMIT to select the first record
14.A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause
15.The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,]
16.SELECT statement includes a LIMIT clause that specifies an offset value of 3 and a row count value of 4
17.WHERE clause can include additional logical operators and expressions that further limit the results returned
18.Limit to first three columns
19.LIMIT 5 OFFSET 3
20.Delete with order by and limit clause
21.Tell MySQL what offset to use, from which result to start limiting.
22.The default offset is 0, so by specifying an offset of 1, you're taking the second record.
23.Selecting Records from the Beginning or End of a Result Set
24.Processing the First or Last n Records
25.Limiting the Number of Results
26.But LIMIT allows you to return only that record