Mysql选择多个ID中的id

时间:2014-03-02 17:20:11

标签: mysql sql

有一个问题,我找不到解决方案。

我有这样的表

CREATE TABLE IF NOT EXISTS `system_tests_run` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `test_id` int(11) NOT NULL,
  `questions_id` text NOT NULL,
  `run_id` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `date_to` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `date_from` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `class` varchar(64) CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
  `minutes` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;



INSERT INTO `system_tests_run` (`id`, `test_id`, `questions_id`, `run_id`, `created`, `date_to`, `date_from`, `class`, `minutes`) VALUES
(1, 1, '1', 1, '2014-03-02 16:59:07', '2014-03-02 17:05:00', '2014-03-02 16:58:00', '1,2,3,4,5,6,7,8,9', 15);

我将此查询用于与表用户连接的加载数据

SELECT r.date_from, 
       r.date_to, 
       r.id, 
       t.name, 
       t.subject, 
       u.meno, 
       u.priezvisko 
  FROM `system_tests_run` r 
       LEFT JOIN system_tests t 
                 ON t.id = r.test_id 
       LEFT JOIN system_users u 
                 ON u.id = r.run_id 
 WHERE r.class IN (1) 
       AND r.date_from <= NOW() 
       AND r.date_to >= NOW();

此表用于在线考试测试,在字段类i中存储类的名称,例如(2),我的查询仅适用于第1类,如果例如在where子句 WHERE r.class IN( 2)所以命令不起作用

如果我使用 WHERE r.class IN(1,2,3,4,5,6,7,8,9),查询工作正常但我只需要一个,因为学生有只有一个班级和班级可以进行定向测试,而不是全部 你想不出任何适合我的解决方案? 谢谢,

1 个答案:

答案 0 :(得分:1)

使用REGEXP,但您应该真正了解normalization

SQL FIDDLE