ster ster - 3 months ago 15
MySQL Question

MySQL concat column in date_format

I have this table: table1, and four varaible

$START_MONTH
$START_YEAR
$END_MONTH
$END_YEAR
which return: month:1, year: 2014 and 3, 2015

+----+-----+-------+------+-------------+
| ID | DAY | MONTH | YEAR | DESCRIPTION |
+----+-----+-------+------+-------------+
| 1 | 1 | 1 | 2014 | Product 1 |
| 2 | 1 | 2 | 2015 | Product 2 |
| 3 | 2 | 3 | 2014 | Product 1 |
| 4 | 10 | 1 | 2015 | Product 3 |
| 5 | 9 | 5 | 2015 | Product 3 |
+----+-----+-------+------+-------------+

SELECT
DATE_FORMAT (CONCAT (DAY,' ', MONTH,' ',YAR), GET_FORMAT(DATE,'EUR')) AS NEW_DATA,
DESCRIPTION

FROM table1

WHERE
NEW_DATA BETWEEN $START_MONTH . $START_YEAR AND $END_MONTH . $END_YEAR


I need return the rows included between: 1.2014 and 3.2015. Thank you!

Answer

In MySQL you can filter by combined columns:

SELECT *
FROM table1
WHERE (`YEAR`, `MONTH`) >= (2014,1)
  AND (`YEAR`, `MONTH`) <= (2015,3)

http://sqlfiddle.com/#!9/87000/2

Comments