0

我有一个包含列idname. 我的重点是id. 事实上,我想选择行,当数字的顺序id中断时。看我的例子:

// mytable
+----+-----------+
| id |    name   |
+----+-----------+
| 1  |   ali     |
| 2  |   jack    |
| 3  |   peter   |
| 5  |   steve   |
| 6  |   lenord  |
| 7  |   jack    |
| 9  |   fered   |
+----+-----------+

现在我想select where id=5select where id=9。因为id=4id=8被删除。

编辑:我想要这个输出:

// mytable
+----+-----------+
| id |    name   |
+----+-----------+
| 5  |   steve   |
| 9  |   fered   |
+----+-----------+

我可以这样做吗?

4

3 回答 3

0

尝试寻找与特定 ID。 http://php.net/manual/en/mysqli-result.data-seek.php

<?php
/* Open a connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT id, name FROM table";
if ($result = $mysqli->query($query)) {

    /* seek to row no. 4 */
    $result->data_seek(3);

    /* fetch row */
    $row = $result->fetch_row();

    printf ("ID: %s  Name: %s\n", $row[0], $row[1]);

    /* seek to row no. 7 */
    $result->data_seek(6);

    /* fetch row */
    $row = $result->fetch_row();

    printf ("ID: %s  Name: %s\n", $row[0], $row[1]);

    /* free result set*/
    $result->close();
}

/* close connection */
$mysqli->close();
?>

这应该返回:

ID: 5 Name: steve
ID: 9 Name: fered
于 2015-06-24T07:17:33.580 回答
0

实际上,您希望 id 的记录仅比缺少的 id 高一个;所以你需要搜索id那些id-1存在的记录(对于最低的id总是这样,所以我们必须明确地排除id=1

SELECT *
FROM the_table tt
WHERE id > 1
AND NOT EXISTS (
   SELECT *
   FROM the_table nx
   WHERE nx.id = tt.id -1
   );
于 2015-06-24T11:27:12.277 回答
0
SELECT x.*
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON y.id = x.id - 1 
 WHERE y.id IS NULL 
   AND x.id > 1;
+----+-------+
| id | name  |
+----+-------+
|  5 | steve | 
|  9 | fered | 
+----+-------+
于 2015-06-25T09:22:09.133 回答