1

给定下表设置:

--
-- 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_picturesor中不存在的缺失 pid 值errors

例如给出:

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 上为两个表设置一个索引以加快速度。

4

1 回答 1

0

这似乎是一件奇怪的事情。为什么不忘记丢失的 pid 并继续前进呢?反正...

    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;
于 2013-01-08T13:38:23.813 回答