1

I'm using Multidatespicker so I have dates in my database as follow:

ID    POST  KEY                  VALUE
1     1     multipledates        20151020, 20151015, 20151101
2     2     multipledates        20151103
3     3     multipledates        20151022, 20151010, 20151202, 20160410
...

Then I have 'date_start' and 'date_end'. I need to select dates between this dates.

This SQL query only works in my example with ID2 (because it hasn't commas)

SELECT ... BETWEEN $date_start AND $date_end

The solution would be to use something like 'LIKE' but not if possible:

SELECT ... BETWEEN LIKE '%$date_start%' AND LIKE '%$date_end%'

Any ideas?

Thanks.

kurtko
  • 1,978
  • 4
  • 30
  • 47

1 Answers1

3

You should redesign your schema for sure.

But just as a study case you can:

http://sqlfiddle.com/#!9/22755/4

SELECT t.id,
   t.date
FROM (SELECT
  t1.id,
  DATE(SUBSTRING_INDEX(SUBSTRING_INDEX(t1.value, ',', numbers.n), ',', -1)) `date`
FROM
  numbers 
  INNER JOIN t1
  ON CHAR_LENGTH(t1.value)
     -CHAR_LENGTH(REPLACE(t1.value, ',', ''))>=numbers.n-1
ORDER BY
  id, n ) t
WHERE t.date BETWEEN $date_start AND $date_end

Notice this solution expects that you have numbers table with record values from 1 to max number of elements in your valuelist column.

(c) code based on https://stackoverflow.com/a/17942691/4421474

Community
  • 1
  • 1
Alex
  • 16,739
  • 1
  • 28
  • 51