Search code examples
mysqlsqlwampserver

multi sql select statement


i have a table of this sort:

| name    |  salary |  day  |  month   |
| james   |  200.00 |    2  | january  |
| marie   |  400.00 |    4  | january  |
| jimmy   |  300.00 |    7  | january  |
| Fredd   |  700.00 |    3  | february |
| rosieli |  500.00 |    5  | february |
| rosela  |  800.00 |    6  | february |

if the table name is 'db_table', how do I write an sql select query to select records from 4th January to 5th February.
something like:

select * from db_table between day='4',month='january' and day='5' and month='february'";

please how do I write a proper sql statement to get the desired results.so that the table looks like this:

| name    |  salary |  day  |  month   | 
| marie   |  400.00 |    4  | january  |
| jimmy   |  300.00 |    7  | january  |
| Fredd   |  700.00 |    3  | february |
| rosieli |  500.00 |    5  | february |

thank you


Solution

  • You really should do this using dates.

    select t.*
    from t
    where str_to_date(concat_ws(2020, month, day), '%Y %M %d') between '2020-01-04' and '2020-02-05';
    

    When possible, date comparisons should be made using dates.

    I used 2020 because it is a leap year, so it will handle February 29th.

    Once you've solved this, you should fix your data model to contain an actual date rather than a month/day combination.