How to search data in column with comma-separated date values?

时间:2018-12-19 11:36:34

标签: mysql

My Table with data. enter image description here

Now row 1,2,4 needed as output:

I try this:

SELECT * FROM my_table 
WHERE SUBSTRING_INDEX(my_table.`dates`, ',', 1) >= '01-01-2018'
AND SUBSTRING_INDEX(my_table.`dates`, ',', - 1) <= '13-02-2018'

But it is not working. Is there any good alternative solution? Please if any than suggest me or help me. I am stuck badly in this issue. Thanks

1 个答案:

答案 0 :(得分:1)

首先,您应该真正考虑修复数据;而不是将它们存储为逗号分隔的值。阅读:Is storing a delimited list in a database column really that bad?

第二,即使使用当前的方法,您也需要注意MySQL Date format YYYY-MM-DD ,而不是 DD-MM-YYYY (就像您存储的值一样)。

您将需要使用Str_to_Date()函数,以便能够进行正确的日期比较。

      'a'  'b'  'c'  'd'  'e'
'a'    0    1    0    1    1
'b'    1    0    1    1    2
'c'    0    1    0    1    1
'd'    1    1    1    0    2
'e'    1    2    1    2    0