我在MySQL中遇到数据问题。 我有一个专栏“vacation_period”,我用逗号分隔数据,例如。 “02/10 / 2015,02 / 11/2015”,我想用月份和年份的逗号分隔。我想创建条件,我可以将POST发送的月份和年份与MySQL字段的爆炸数据进行比较。
示例查询:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
ag_vacations.vacation_period = 2015 AND
ag_vacations.vacation_period = 02
order by date_added desc
答案 0 :(得分:1)
虽然一串日期并不理想,但我们有时会以这种方式接收数据。如果您需要在有机会清理架构之前进行查询,并且日期格式一致,则可以使用like
查询字符串。在此查询中,我们将concat
逗号添加到ag_vacations.vacation_period
列的开头和结尾,以确保我们获得月份日期的开头和年份日期的结束:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
concat(ag_vacations.vacation_period,',') like '%/2015,%' AND
concat(',',ag_vacations.vacation_period like '%,02/%'
order by date_added desc
答案 1 :(得分:1)
所以试试这个:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
((ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/2015\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}' AND
ag_vacations.vacation_period REGEXP '02\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}')
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}'))
order by date_added desc
所以,如果您需要检查该字符串02/10/2015,02/11/2015
中的第一个日期 - 您可以删除OR部分:
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}')
答案 2 :(得分:0)
如果您只是想找到该行,则不需要在php中爆炸。您可以使用FIND_IN_SET
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set