简体   繁体   English

MySQL会针对基于日期的事件(具有自定义日期格式)以及MySQL条件(例如事件通过日期)进行事件的MySQL提取

[英]Issue with MySQL fetch for events based on date with custom date format and with MySQL condition like events passing through dates

I am loading all events and displaying it on a calender month wise. 我正在加载所有事件,并在每个月的日历上显示它。 When i click on a day like 21-07-2014 it will fetch all records in that date and show it no the right side of calender via ajax. 当我单击2014年7月21日这样的日期时,它将获取该日期中的所有记录,并且不会通过ajax显示日历的右侧。 What i am trying to achieve is that, i want to fetch all events in 21-07-2014 and also events having start at 10-07-2014 and ends on ex: '31-07-2014'(Any events passing through given event date) 我要达到的目的是,我想获取2014年7月21日的所有事件,以及自2014年10月7日开始并以例如“ 31-07-2014”结束的事件(所有事件都通过给定活动日期)

Event start date field is event_date and event end date field is end_date 事件开始日期字段为event_date,事件结束日期字段为end_date

Event Table Structure: 事件表结构:

CREATE TABLE IF NOT EXISTS `events` (
  `event_id` int(11) NOT NULL,
  `event_title` varchar(250) NOT NULL,
  `event_date` datetime NOT NULL,
  `end_date` datetime NOT NULL,
  `location` varchar(100) DEFAULT NULL,
  `description` text,
  `event_photo` varchar(50) DEFAULT NULL,
  `thumbnail` text NOT NULL,
  `photo` text NOT NULL,
  `user_id` int(11) NOT NULL,
  `created_date` datetime DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `modified_date` datetime DEFAULT NULL,
  `modified_by` int(11) DEFAULT NULL,
  `event_share_type` enum('1','0') DEFAULT NULL,
  `delete_status` enum('1','0') DEFAULT '0',
  `remarks` varchar(250) NOT NULL,
  `status` enum('1','0') DEFAULT '1',
  PRIMARY KEY (`event_id`) USING BTREE,
  KEY `delete_status` (`delete_status`) USING BTREE,
  KEY `event_id` (`event_id`,`user_id`,`created_date`,`delete_status`,`status`,`event_share_type`,`end_date`) USING BTREE,
  KEY `remarks` (`remarks`) USING BTREE,
  KEY `status` (`status`) USING BTREE,
  KEY `user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

This is my Mysql Query: 这是我的Mysql查询:

SELECT DISTINCT `Events`.`event_id` as id, `Events`.`event_title` AS `title`,
'Events`.`event_date` AS `start`, `Events`.`end_date` AS `end` FROM `events` AS `Events` 
WHERE ((`Events`.`end_date` >= DATE("2014-07-10")) OR (`Events`.`event_date` LIKE '%2014-07-10%') OR (`Events`.`end_date` LIKE '%2014-07-10%')) AND `Events`.`delete_status` = '0' AND `Events`.`status` = '1' ORDER BY `Events`.`created_date` ASC;

This is CakePhp find: 这是CakePhp查找:

$this->Events->find('all', array(
            'joins'=>$joins,
            'conditions' => array(
                'OR'=>array('Events.end_date >= DATE("'.$dateFormat.'")','Events.event_date LIKE' => "%".$dateFormat."%",'Events.end_date LIKE' => "%".$dateFormat."%")
                ,'Events.delete_status'=>'0','Events.status'=>'1','CP.circuit_id IN '.$my_circuits,'CP.circuit_post_id' => 2,'CP.delete_status'=>'0','CP.status'=>'Y'),
            'order'=>'Events.created_date ASC',  
            'fields'=>array('DISTINCT Events.event_id as id','Events.event_title as title','Events.event_date as start','Events.end_date as end')
        ));

It sounds like you are looking for events that start sometime before the end (23:59:59) of the selected day and end sometime after the start (00:00:00) of the selected day. 听起来您正在寻找的事件是在所选日期的结束(23:59:59)之前的某个时间开始,并在所选日期的开始(00:00:00)之后的某个时间结束的事件。

Try these adjustments to your cakephp query: 尝试对您的cakephp查询进行以下调整:

$conditions = array('Events.event_date <' => $dateFormat.' 23:59:59',
                    'Events.end_date >' => $dateFormat.' 00:00:00',
                    'Events.delete_status' => '0',
                    'Events.status' => '1'
                    'CP.circuit_id IN '.$my_circuits,
                    'CP.circuit_post_id' => 2,
                    'CP.delete_status' => '0'
                    'CP.status'=>'Y');
$this->Events->find('all', array(
            'joins' => $joins,
            'conditions' => $conditions,
            'order'=>'Events.created_date ASC',  
            'fields'=>array('DISTINCT Events.event_id as id','Events.event_title as title','Events.event_date as start','Events.end_date as end')
        ));

Also according to the cakephp documentation if you can turn $my_circuits into an array you could use this line in the condition instead of the 'CP.circuit_id IN '.$my_circuits SQL fragment. 同样根据cakephp文档,如果可以将$ my_circuits转换为数组,则可以在条件中使用此行,而不是'CP.circuit_id IN '.$my_circuits SQL片段。

"CP.circuit_id" => $my_circuits

The demonstration given in the documentation is: 文档中给出的演示是:

Let's say you wanted to find posts where the title was in a given set of values: 假设您要查找标题在给定值集中的帖子:

array(
    "Post.title" => array("First post", "Second post", "Third post")
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM