PHP 如何设置脚本从两个字段中获取两个数值,将它们添加并更新到第三个字段

发布于 2024-10-19 16:29:40 字数 560 浏览 2 评论 0原文

我有一个包含各种字段的表,其中 2 个字段包含 1 到 50 之间的数字。

我需要帮助设置一个脚本,该脚本将自动将两个设置字段添加起来,并将总计插入到每个单独记录的第三个字段中,以获取所有记录桌子。

我一直在创建这个,因为有些记录会有 0 或 NULL 值。

请帮助:)

我认为我可以纯粹用 SQL 来完成此操作,但我发现的最接近的是:

Alter table Mytable add column Keywords varchar(255);
update Mytable set Keywords = concat(Categories, ' ', Tags);

显然我不需要添加列等。PHP 或 SQL 解决方案都很棒。

举个例子,以防我没有很好地解释这一点,这让我很困惑,

Column 1 - Field Value: 20
Colum 2 - Field Value 20
Add Colum 1 + Colum 2 = 40
Insert 40 into Colum 3

谢谢

I have a table with various fields 2 of which contain a number between 1 and 50.

I need help setting up a script that will automatically take two set fields add them and insert the total into a third field for each indivdual record for all records in the table.

I'm stuck in creating this because some records will have 0 or NULL values.

Please help :)

I've think I may be able to do this purely in SQL but the closest i found was this:

Alter table Mytable add column Keywords varchar(255);
update Mytable set Keywords = concat(Categories, ' ', Tags);

Obviously I would not need to add the column etc. Either a PHP or SQL solution would be fantastic.

Example in case I'mm not explaining this well it's confusing me a lot

Column 1 - Field Value: 20
Colum 2 - Field Value 20
Add Colum 1 + Colum 2 = 40
Insert 40 into Colum 3

Thanks

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

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

发布评论

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

评论(4

一曲爱恨情仇 2024-10-26 16:29:40

您应该使用纯 SQL 来执行此操作。

如果两列都存储为数字:

UPDATE tableName 
SET `Column 3` = IFNULL(`Column 1`,0) + IFNULL(`Column 2`,0)

如果它们存储为字符:

UPDATE tableName
SET `Column 3` = CAST(IFNULL(`Column 1`,0) AS SIGNED)
               + CAST(IFNULL(`Column 2`,0) AS SIGNED)

You should do this in plain SQL.

If both columns are stored as numbers:

UPDATE tableName 
SET `Column 3` = IFNULL(`Column 1`,0) + IFNULL(`Column 2`,0)

If they are stored as characters:

UPDATE tableName
SET `Column 3` = CAST(IFNULL(`Column 1`,0) AS SIGNED)
               + CAST(IFNULL(`Column 2`,0) AS SIGNED)
拥有 2024-10-26 16:29:40

尝试以下操作:

UPDATE Mytable SET column3 = column2 + column1

这应该适用于 0 ,但是如果您在任何一个中都有 NULL 值
添加列,您将在结果字段中得到 NULL,因此在这种情况下,查询会略有不同,

例如 if
Column1 可能有空值,那么查询应该是

UPDATE Mytable SET column3 = IFNULL(column1 ,0) + column2

如果两个字段都可以有 NULL 值,那么

UPDATE Mytable SET column3 = IFNULL(列1,0) + IFNULL(列2,0)

Try the following:

UPDATE Mytable SET column3 = column2 + column1

this should work for 0 , however if you have got NULL values in any of the
adding column you will get a NULL in the result field, so in that case the query is slightly different

for e.g. if
Column1 may have null values, then the query should be

UPDATE Mytable SET column3 = IFNULL(column1 ,0) + column2

and if both the fields can have NULL values then,

UPDATE Mytable SET column3 = IFNULL(column1 ,0) + IFNULL(column2 ,0)

盛夏尉蓝 2024-10-26 16:29:40

这应该可以

UPDATE [TABLE_NAME] SET col3 = [TABLE_NAME].col1 + [TABLE_NAME].col2;

用您的表和列替换 [TABLE_NAME] 和 col1、col2、col3

This should do it

UPDATE [TABLE_NAME] SET col3 = [TABLE_NAME].col1 + [TABLE_NAME].col2;

Replace [TABLE_NAME] and col1, col2, col3 with your tables and columns

看轻我的陪伴 2024-10-26 16:29:40
UPDATE your_table SET Column3 = Column1+Column2

或者

UPDATE your_table SET Column3 = IFNULL ( Column1, 0 ) + Column2

避免 Column1 上出现 NUL

UPDATE your_table SET Column3 = Column1+Column2

or

UPDATE your_table SET Column3 = IFNULL ( Column1, 0 ) + Column2

to avoid NULs on Column1

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