MySQL – How to SELECT Rows with Time Duration in Varchar Column

MySQLmysql-5.5PHPtime

There is a column in table defined like this :

duration varchar(10)

It has value for duration of a MP3 file stored in it like : 52:19 (Minutes : Seconds)

How can I create a SQL query, for selecting rows with duration between 3:00 and 6:45 (i.e. SELECT songs neither too short nor too long)

The database is hosted on some 3rd party server so I cannot modify table structure.

Best Answer

SELECT   * FROM table
WHERE (SUBSTRING_INDEX(duration,':',1)*60)+SUBSTRING_INDEX(duration,':',-1) 
BETWEEN 180 AND 405

SQL Fiddle

Use SUBSTRING_INDEX to isolate the minutes and seconds,convert minutes to seconds and add them up so the where condition has something to compare.Assuming all durations in the table have minutes and seconds separated by ':'