检测来自2个表的组合的数据中的间隙

时间:2013-01-08 12:44:51

标签: php mysql sql grouping

鉴于以下表格设置:

--
-- Table structure for table `errors`
--

CREATE TABLE IF NOT EXISTS `errors` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(255) NOT NULL,
  `num` int(11) NOT NULL,
  `pid` bigint(20) unsigned NOT NULL,
  `error` varchar(512) NOT NULL,
  `datetime` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=190441 ;

-- --------------------------------------------------------

--
-- Table structure for table `stored_pictures`
--

CREATE TABLE IF NOT EXISTS `stored_pictures` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(255) NOT NULL,
  `pid` bigint(20) unsigned NOT NULL,
  `num` int(11) NOT NULL,
  `updated_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `picture_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_url` (`url`),
  KEY `idx_picture_id` (`picture_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2145543 ;

-- --------------------------------------------------------

我需要能够处理stored_pictureserrors

中不存在的丢失的pid值

例如:

stored_pictures pids:
1
5
6
7
8
9
15
19
20
21
22
23
24
25

errors pids:
2
4
14
17

我需要获得一份清单:

3
10
11
12
13
16
18

然后将它们放入php数组进行处理。我也在想我需要在两个表的pid上设置一个索引来加快速度。

1 个答案:

答案 0 :(得分:0)

这似乎是一件奇怪的事情。为什么不忘记丢失的pids并继续前进?总之...

    CREATE TABLE ints(i INT NOT NULL PRIMARY KEY);

    INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

    CREATE TABLE stored_pictures (pid INT NOT NULL PRIMARY KEY);

    INSERT INTO stored_pictures VALUES (1),(5),(6),(7),(8),(9),(15),(19),(20),(21),(22),(23),(24),(25);

    CREATE TABLE errors (pid INT NOT NULL PRIMARY KEY);

    INSERT INTO errors VALUES(2),(4),(14),(17);

    SELECT i3.i*100 + i2.i*10 + i1.i + 1 missing_pid
      FROM ints i1
      JOIN ints i2
      JOIN ints i3
      LEFT
      JOIN 
         ( SELECT pid FROM stored_pictures
            UNION
           SELECT pid FROM errors
         ) a 
        ON a.pid = i3.i*100 + i2.i*10 + i1.i + 1
      JOIN
         ( SELECT MAX(pid) max_pid 
             FROM 
                ( SELECT pid FROM stored_pictures
                   UNION
                  SELECT pid FROM errors
                ) x
         ) b
        ON b.max_pid >= i3.i*100 + i2.i*10 + i1.i + 1
     WHERE a.pid IS NULL;