Select distinct values between two values in different field I have a table named products like this:

| product_name | start_year | end_year |
|--------------|------------|----------|
| wine         | 2001       | 2012     |
| cheese       | 1999       | 2010     |
| apple        | 2008       | 2015     | 


What kind of query do I have to run to have all values between start_year and  end_year where product = 'apple'?

This is the desired results:

| year |
|------|
| 2008 |
| 2009 |
| 2010 |
| 2011 |
| 2012 |
| 2013 |
| 2014 |
| 2015 |
PROPOSED QUERY

SELECT B.num year FROM
(SELECT start_year,end_year FROM products WHERE product_name='apple') A,
(SELECT tth*10000+th*1000+h*100+t*10+u num FROM
(SELECT 0 tth UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) TTHN,
(SELECT 0 th UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) THN,
(SELECT 0 h UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) HN,
(SELECT 0 t UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) TN,
(SELECT 0 u UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) UN) B
WHERE num BETWEEN A.start_year AND A.end_year ORDER BY B.num;


YOUR SAMPLE DATA

mysql&gt; DROP DATABASE IF EXISTS EmaOnTheBlock;
Query OK, 1 row affected (0.02 sec)

mysql&gt; CREATE DATABASE EmaOnTheBlock;
Query OK, 1 row affected (0.00 sec)

mysql&gt; USE EmaOnTheBlock
Database changed
mysql&gt; CREATE TABLE products
    -&gt; (id int not null auto_increment,
    -&gt; product_name varchar(20),
    -&gt; start_year smallint not null,
    -&gt; end_year smallint not null,
    -&gt; primary key (id));
Query OK, 0 rows affected (0.02 sec)

mysql&gt; INSERT INTO products (product_name,start_year,end_year)
    -&gt; VALUES ('wine',2001,2012),('cheese',1999,2010),('apple',2008,2015);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql&gt; SELECT * FROM products;
+----+--------------+------------+----------+
| id | product_name | start_year | end_year |
+----+--------------+------------+----------+
|  1 | wine         |       2001 |     2012 |
|  2 | cheese       |       1999 |     2010 |
|  3 | apple        |       2008 |     2015 |
+----+--------------+------------+----------+
3 rows in set (0.00 sec)

mysql&gt;


PROPOSED QUERY EXECUTED

mysql&gt; SELECT B.num year FROM
    -&gt; (SELECT start_year,end_year FROM products WHERE product_name='apple') A,
    -&gt; (SELECT tth*10000+th*1000+h*100+t*10+u num FROM
    -&gt; (SELECT 0 tth UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
    -&gt; UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) TTHN,
    -&gt; (SELECT 0 th UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
    -&gt; UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) THN,
    -&gt; (SELECT 0 h UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
    -&gt; UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) HN,
    -&gt; (SELECT 0 t UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
    -&gt; UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) TN,
    -&gt; (SELECT 0 u UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
    -&gt; UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) UN) B
    -&gt; WHERE num BETWEEN A.start_year AND A.end_year ORDER BY B.num;
+------+
| year |
+------+
| 2008 |
| 2009 |
| 2010 |
| 2011 |
| 2012 |
| 2013 |
| 2014 |
| 2015 |
+------+
8 rows in set (0.03 sec)

mysql&gt;


GIVE IT A TRY !!!