需要帮助来纠正两个表中的MySQL查询结果

时间:2014-09-28 23:39:05

标签: mysql sql

我有两个表,一个是事件,一个是句点。我想在一个查询中选择这两个表数据。这两个表有一个共同的字段start_time,我希望结果按该列排序。

表的表结构:

CREATE TABLE IF NOT EXISTS `events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL,
  `is_repeated` tinyint(4) NOT NULL DEFAULT '0',
  `alarm_type` enum('set_date','days','monthly') COLLATE utf8_unicode_ci NOT NULL,
  `event_alarm_date` date DEFAULT NULL,
  `alarm_days` set('Sat','Sun','Mon','Tue','Wed','Thu','Fri') COLLATE utf8_unicode_ci DEFAULT NULL,
  `monthly_alarm_days` set('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31') COLLATE utf8_unicode_ci DEFAULT NULL,
  `alarm_audio` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `count_down_time` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;

--
-- Dumping data for table `events`
--

INSERT INTO `events` (`id`, `event_name`, `start_time`, `end_time`, `is_repeated`, `alarm_type`, `event_alarm_date`, `alarm_days`, `monthly_alarm_days`, `alarm_audio`, `created_on`, `count_down_time`) VALUES
(1, 'Lunch Break', '10:30:45', '11:30:45', 1, 'days', NULL, 'Mon,Wed', NULL, '1411587500.mp3', '0000-00-00 00:00:00', 0),
(2, 'Openning Ceremony', '10:30:45', '11:30:45', 1, 'monthly', NULL, NULL, '2,3,4', '1411587568.mp3', '0000-00-00 00:00:00', 0),
(3, 'Inspection', '10:30:45', '12:45:30', 0, 'set_date', '2014-09-26', NULL, NULL, '1411587695.mp3', '0000-00-00 00:00:00', 0),
(5, 'Test2', '10:30:45', '11:30:45', 1, 'monthly', NULL, NULL, '3,4,5', '1411595801.mp3', '2014-09-24 21:56:41', 0),
(6, 'Test3', '22:20:30', '23:25:30', 1, 'days', NULL, 'Sun,Mon', NULL, '1411597086.mp3', '2014-09-24 22:18:06', 0);

CREATE TABLE IF NOT EXISTS `periods` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `period_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `start_time` time NOT NULL,
  `repeat_on` set('Sat','Sun','Mon','Tue','Wed','Thu','Fri') COLLATE utf8_unicode_ci DEFAULT NULL,
  `count_down_time` tinyint(4) NOT NULL DEFAULT '40',
  `created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `period_audio` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

--
-- Dumping data for table `periods`
--

INSERT INTO `periods` (`id`, `period_name`, `start_time`, `repeat_on`, `count_down_time`, `created_on`, `period_audio`) VALUES
(1, 'Quran Recitation', '08:15:00', 'Mon,Wed', 30, '2014-09-25 01:30:26', ''),
(2, 'Morning Assembly', '08:15:00', 'Sun,Mon,Wed', 30, '2014-09-28 04:25:24', '');

我在下面写过查询。此查询获取8条记录,而根据表数据,它应获得5条记录。

SELECT pr.period_name, 
     pr.start_time as period_start_time, 
     pr.repeat_on, pr.created_on as period_created_on, 
     pr.count_down_time as period_count_down_time, 
     pr.period_audio, ev.event_name, 
     ev.start_time as event_start_time, 
     ev.end_time as event_end_time, 
     ev.is_repeated, ev.alarm_type, 
     ev.event_alarm_date,ev.alarm_days, 
     ev.monthly_alarm_days, 
     ev.alarm_audio, 
     ev.created_on as event_created_on, 
     ev.count_down_time as event_count_down_time 
 FROM periods as pr, events as ev  
 WHERE 
    FIND_IN_SET('Mon',repeat_on)>0 AND

    CASE alarm_type WHEN 'set_date' THEN FIND_IN_SET('2014-09-25',event_alarm_date)>0 
                        WHEN 'days' THEN FIND_IN_SET('Mon',alarm_days) > 0 
                        WHEN 'monthly' THEN FIND_IN_SET('2',monthly_alarm_days) > 0 
        END = 1
  ORDER BY pr.start_time AND ev.start_time

任何帮助表示感谢。

0 个答案:

没有答案