Php 从两个表的组合中检测数据中的差距

Php 从两个表的组合中检测数据中的差距,php,mysql,sql,grouping,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) N

给定下表设置:

--
-- 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 ;

-- --------------------------------------------------------
我需要能够处理存储的图片或错误中不存在的缺失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上为两个表设置一个索引以加快速度。

这似乎是一件奇怪的事情。为什么不忘记丢失的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;