选择最后一个 id,不插入

发布于 2024-09-07 07:27:37 字数 272 浏览 7 评论 0原文

我正在尝试检索一个表 A 的 id 以插入到另一个表 B 中。我无法使用 last_insert_id(),因为我没有在 A 中插入任何内容。关于如何很好地做到这一点有什么想法吗?

$n = mysql_query("SELECT max(id) FROM tablename"); 似乎不起作用,也不起作用

$n = mysql_query("SELECT max(id) FROM tablename GROUP BY id");

I'm trying to retrieve the id of one table A to insert into another table B. I cannot use last_insert_id() as i have not inserted anything into A. Any ideas on how to do this nicely?

$n = mysql_query("SELECT max(id) FROM tablename"); doesn't seem to work, nor does

$n = mysql_query("SELECT max(id) FROM tablename GROUP BY id");

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

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

发布评论

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

评论(6

行至春深 2024-09-14 07:27:37

在 MySQL 中,这确实返回 id 列中的最高值:

SELECT MAX(id) FROM tablename;

但是,这不会将该 id 放入 $n 中:

$n = mysql_query("SELECT max(id) FROM tablename");

要获取该值,您需要执行以下操作:

$result = mysql_query("SELECT max(id) FROM tablename");

if (!$result) {
    die('Could not query:' . mysql_error());
}

$id = mysql_result($result, 0, 'id');

如果你想从A中获取最后一次插入的ID,并将其插入到B中,你可以用一条命令来完成:

INSERT INTO B (col) SELECT MAX(id) FROM A;

In MySQL, this does return the highest value from the id column:

SELECT MAX(id) FROM tablename;

However, this does not put that id into $n:

$n = mysql_query("SELECT max(id) FROM tablename");

To get the value, you need to do this:

$result = mysql_query("SELECT max(id) FROM tablename");

if (!$result) {
    die('Could not query:' . mysql_error());
}

$id = mysql_result($result, 0, 'id');

If you want to get the last insert ID from A, and insert it into B, you can do it with one command:

INSERT INTO B (col) SELECT MAX(id) FROM A;
源来凯始玺欢你 2024-09-14 07:27:37

您可以按 id 对表进行降序排序,并将结果数量限制为 1:

SELECT id FROM tablename ORDER BY id DESC LIMIT 1

BUT ORDER BY 会为此请求重新排列整个表。所以如果你有很多数据并且需要多次重复这个操作,我不会推荐这个解决方案。

You could descendingly order the tabele by id and limit the number of results to one:

SELECT id FROM tablename ORDER BY id DESC LIMIT 1

BUT: ORDER BY rearranges the entire table for this request. So if you have a lot of data and you need to repeat this operation several times, I would not recommend this solution.

多情癖 2024-09-14 07:27:37

我有不同的解决方案:

SELECT AUTO_INCREMENT - 1 as CurrentId FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'tablename'

I have different solution:

SELECT AUTO_INCREMENT - 1 as CurrentId FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'tablename'
鯉魚旗 2024-09-14 07:27:37

您可以获取最大列值并递增它< /a>:

InnoDB使用以下算法来
初始化自动递增计数器
对于包含一个表 t
名为 ai_col 的 AUTO_INCRMENT 列:
服务器启动后,首先
插入表t,InnoDB执行
相当于此语句:

从 t 中选择 MAX(ai_col) 进行更新;

InnoDB 将值加一
通过语句检索并赋值
它到列和到
表的自动增量计数器。
如果表为空,InnoDB 使用
值1。

您也可以使用 显示表状态 及其“Auto_increment”值。

You can get maximum column value and increment it:

InnoDB uses the following algorithm to
initialize the auto-increment counter
for a table t that contains an
AUTO_INCREMENT column named ai_col:
After a server startup, for the first
insert into a table t, InnoDB executes
the equivalent of this statement:

SELECT MAX(ai_col) FROM t FOR UPDATE;

InnoDB increments by one the value
retrieved by the statement and assigns
it to the column and to the
auto-increment counter for the table.
If the table is empty, InnoDB uses the
value 1.

Also you can use SHOW TABLE STATUS and its "Auto_increment" value.

安穩 2024-09-14 07:27:37

我想为每条记录添加时间戳并获取最新的。在这种情况下,您可以获得任何 id、打包行和其他操作。

I think to add timestamp to every record and get the latest. In this situation you can get any ids, pack rows and other ops.

白首有我共你 2024-09-14 07:27:37
echo $con->query("SELECT max(id) as max FROM table")->max;

或者

$maxid = $con->query("SELECT max(id) as max FROM table")->max;

echo $maxid;
echo $con->query("SELECT max(id) as max FROM table")->max;

or

$maxid = $con->query("SELECT max(id) as max FROM table")->max;

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