检查 mysql ID 跳过 php

发布于 2024-11-28 08:39:44 字数 179 浏览 3 评论 0原文

假设在 mysql 中我有一个 id 列,只是 int 自动增量。

有什么方法可以使用 php 告诉我何时有一个 id 被删除吗?

就像说我有

5 个 6 7 8 10

我需要它告诉我 9 丢失了,并对所有 id 继续执行此操作。有什么办法可以做到这一点吗?

谢谢

say in mysql I have a column that's id, just int auto increment.

Is there any way I can use php to tell me when there is an id that's been removed?

Like say i have

5
6
7
8
10

I need it to tell me that the 9 is missing, and continue this for all the id's. Is there any way to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仲春光 2024-12-05 08:39:44

您需要一个包含所有值的表。然后很容易进行左连接来查找丢失的 id。

create table all_values
(id int not null) engine = myisam;

insert into all_values (id) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

create table existing_values like all_values;

insert into existing_values (id) values (5),(6),(7),(8),(10);

select a.id from all_values as a
left join existing_values as b
on a.id = b.id
where b.id is null

You need a table that contains all values. Then it's simple to do a left join to find missing ids.

create table all_values
(id int not null) engine = myisam;

insert into all_values (id) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

create table existing_values like all_values;

insert into existing_values (id) values (5),(6),(7),(8),(10);

select a.id from all_values as a
left join existing_values as b
on a.id = b.id
where b.id is null
聽兲甴掵 2024-12-05 08:39:44

这是一种方法:
1. 从表中选择所有 ID。
2. 将它们全部分配给 %hash:
5=> 1
6=> 1
7=> 1
8=> 1
10=> 1

所以你的 ID 是哈希的键,值为 1。

  1. 创建一个 for 循环

for (my $i = 1; $i < 11; $i++)
{
   if($hash{$i} != 1)
   {
      打印“ID $i 丢失\n”;
   }
}

这是 Perl 语法,但 PHP 应该非常相似。

Here is one way to do it:
1. Select all IDs from the table.
2. Assing them all to a %hash:
5 => 1
6 => 1
7 => 1
8 => 1
10 = > 1

So your ID is a key of the hash and the value is 1.

  1. Create a for loop

for (my $i = 1; $i < 11; $i++)
{
   if($hash{$i} != 1)
   {
      print "ID $i is missing \n";
   }
}

This is Perl syntax, but PHP should be pretty similar.

萌逼全场 2024-12-05 08:39:44

如果您可以忍受数据库中第一个 id 之前没有间隙(以及使用 '临时表'、'文件排序'和'连接缓冲区'):

SELECT
    a.id + 1 AS `From`,
    MIN(b.id - 1) AS `To`
FROM
    foo as a, foo as b
WHERE
    a.id < b.id
GROUP BY
    a.id
HAVING
    a.id < min(b.id) -1

独立示例:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// usually I create temporary tables for example scripts, but
// self-joins don't work on temp tables in mysql, bare me....
$pdo->exec('CREATE TABLE soFoo ( id int, primary key(id))');
$pdo->exec('INSERT INTO soFoo (id) VALUES (5),(6),(7),(8),(10),(15),(21)');

foreach( $pdo->query('
    SELECT
        a.id + 1 AS `From`,
        MIN(b.id - 1) AS `To`
    FROM
        foo as a, foo as b
    WHERE
        a.id < b.id
    GROUP BY
        a.id
    HAVING
        a.id < min(b.id) -1', PDO::FETCH_ASSOC) as $row
) {
    echo $row['From'], ' - ', $row['To'], "\n";
}

打印

9 - 9
11 - 14
16 - 20

If you can live with not getting a gap before the first id in the database (and a query using 'temporary table', 'filesort' and 'join buffer'):

SELECT
    a.id + 1 AS `From`,
    MIN(b.id - 1) AS `To`
FROM
    foo as a, foo as b
WHERE
    a.id < b.id
GROUP BY
    a.id
HAVING
    a.id < min(b.id) -1

self-contained example:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// usually I create temporary tables for example scripts, but
// self-joins don't work on temp tables in mysql, bare me....
$pdo->exec('CREATE TABLE soFoo ( id int, primary key(id))');
$pdo->exec('INSERT INTO soFoo (id) VALUES (5),(6),(7),(8),(10),(15),(21)');

foreach( $pdo->query('
    SELECT
        a.id + 1 AS `From`,
        MIN(b.id - 1) AS `To`
    FROM
        foo as a, foo as b
    WHERE
        a.id < b.id
    GROUP BY
        a.id
    HAVING
        a.id < min(b.id) -1', PDO::FETCH_ASSOC) as $row
) {
    echo $row['From'], ' - ', $row['To'], "\n";
}

prints

9 - 9
11 - 14
16 - 20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文