php 对数据库中的每条记录执行一些操作

发布于 2024-09-28 16:05:51 字数 251 浏览 2 评论 0原文

我的数据库中有两个表(videos 和 viewData)。

我正在尝试构建一个针对“视频”表中的每条记录运行的脚本,并使用“视频”表中该特定条目的“videoID”字段执行某些操作。 “做某事”部分会将一些数据转储到 viewData 表中。

在调用循环之前我需要将所有记录存储在数组中吗?像这样的循环示例将非常有帮助。另外,如果“视频”表中有 1000 多条记录,那么这种方式也可能具有可扩展性,不会对服务器造成太大影响。

谢谢, 戴夫

I have two tables in the database(videos and viewData) .

Im trying to build a script that runs for each record in the "videos" table and does something using the "videoID" field for that specific entry in the "videos" table. The does something part would be dumping some data into the viewData table.

Would I need to store all the records in an array before calling the loop? An example of a loop like this would be really helpful. Also in a way that could be potentially scalable that wouldn't hurt the server too much if there were a 1000+ records in the "videos" table.

Thanks,
Dave

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

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

发布评论

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

评论(3

仄言 2024-10-05 16:05:51

不惜一切代价尝试避免循环。考虑基于集合的处理,这意味着在一个 SQL 命令中处理整个行集。

我不完全确定你想做什么,因为你的问题有点模糊。但是,这里有两种可能的方法可以使用基于集合的思维来处理您尝试做的事情。

您可以在 UPDATE 中执行 JOIN,本质上是从父表中进行选择,并在单个 UPDATE 命令中更新子表中的所有行。

UPDATE c
    SET Col1=p.Col1
    FROM ParentTable           p
        INNER JOIN ChildTable  c On p.ParentID=c.ParentID
    WHERE ...

您还可以基于 SELECT 进行 INSERT,因此您可以从 SELECT 中返回的每一行创建一行,例如:

INSERT INTO ChildTable
        (Col1, Col2, Col3, Col4)
    SELECT
        p.ColA, p.ColB, 'constant value', p.ColC-p.ColD
        FROM ParentTable p
        WHERE... 

Try to avoid the loop at all costs. Think set based processing, which means handle the entire set of rows within one SQL command.

I'm not entirely sure what you are attempting to do, as your question is a little vague. however, here are two possibly ways to handle what you are trying to do using set based thinking.

You can do a JOIN in an UPDATE, essentially selecting from the parent table and UPDATEing the child table for all rows in a single UPDATE command.

UPDATE c
    SET Col1=p.Col1
    FROM ParentTable           p
        INNER JOIN ChildTable  c On p.ParentID=c.ParentID
    WHERE ...

you can also INSERT based on a SELECT, so you would create one row from each row returned in the SELECT, like:

INSERT INTO ChildTable
        (Col1, Col2, Col3, Col4)
    SELECT
        p.ColA, p.ColB, 'constant value', p.ColC-p.ColD
        FROM ParentTable p
        WHERE... 
浪荡不羁 2024-10-05 16:05:51

在循环中使用数据库并不是一个好的做法。最好通过一次查询将所有表数据选择到一个数组中,并在将来使用该数组。

Working with a database in the loop isn't a good practice. It is good to select all table data into an array by one query and work with this array in future.

毁我热情 2024-10-05 16:05:51

您是否可以通过其他方式访问 MySQL 表?就像使用 MySQL Administrator 或其他工具一样,甚至通过命令行?

这是因为通过查询或数据库函数直接在数据库中执行此操作会花费更多的时间、资源和其他一切。

我会这样做。

但为了清楚起见,除非您将视频本身存储在数据库表中,否则 1000 条记录不是问题。也许是一万个。

一般提示:只做您需要做的事情。

如果您需要对数据进行操作,请在数据库上执行此操作。
如果您需要检查一张表中的一个字段,请使用SELECT your_field FROM your_table而不是SELECT * FROM your_table

Do you have access by other means to MySQL tables? Like with MySQL Administrator or another tool, even by command line?

This is because it would be much more time, resources and everything else, doing that directly in the database, through a query or a database function.

I would do that this way.

But for the sake of clarity, unless you are storing the videos themselves inside database tables, 1000 records are not a problem. Maybe 10,000 would be.

General tip: do just what you need to do.

If you only need to operate upon data, do this on the database.
If you only need to check one field in one table, use SELECT your_field FROM your_table instead of SELECT * FROM your_table.

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