gpt4 book ai didi

php - Sql - 仅在列的第一个空单元格中插入值

转载 作者:行者123 更新时间:2023-11-29 05:25:57 24 4
gpt4 key购买 nike

我有一个 100 行的 Mysql 表,id 是主键。我有一个 column_A,其中有一些未填充的单元格,只有空值。现在,我想要一个将值插入第一个空单元格的 sql 语句。如果我再次运行 php 脚本,它就会插入下一个空单元格等。像这样的东西:

$sql = "update my_TABLE, only affect the first COLUMN_A,row_i that is null

设置 COLUMN_A,row_i=20";

例子:

COLUMN_A

6
7
null
null
null

运行脚本后,

COLUMN_A

6
7
20
null
null

最佳答案

你可以这样做:

UPDATE table1
SET column_a = 20
WHERE column_a IS NULL
LIMIT 1;

但是你真的应该有某种排序依据来真正得到你想要的行,就像下面的例子:

UPDATE table1
SET column_a = 25
WHERE column_a IS NULL
ORDER BY id ASC
LIMIT 1;

sqlfiddle demo

关于php - Sql - 仅在列的第一个空单元格中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com