我如何使用 SQL 和 PHP 更新这些表条目?

发布于 2024-09-02 20:49:25 字数 553 浏览 3 评论 0原文

我正在解决资产数据库问题。

我将资产输入数据库。每个对象都是资产,并且在资产表中具有变量。对象也是一种资产。在此示例中,类型为服务器。

这是检索所有必要数据的查询:

SELECT asset.id
,asset.company
,asset.location
,asset.purchaseDate
,asset.purchaseOrder
,asset.value
,asset.type
,asset.notes
,server.manufacturer
,server.model
,server.serialNumber
,server.esc
,server.warranty
,server.user
,server.prevUser
,server.cpu
,server.memory
,server.hardDrive
FROM asset
LEFT JOIN server
    ON server.id = asset.id
WHERE asset.id = '$id'

然后我将所有结果分配到单个 php 变量中。

我将如何编写查询/脚本来更新资产?

I am working on an Asset Database problem.

I enter assets into a database. Every object is an asset and has variables within the asset table. An object is also a type of asset. In this example the type is server.

Here is the Query to retrieve all necessary data:

SELECT asset.id
,asset.company
,asset.location
,asset.purchaseDate
,asset.purchaseOrder
,asset.value
,asset.type
,asset.notes
,server.manufacturer
,server.model
,server.serialNumber
,server.esc
,server.warranty
,server.user
,server.prevUser
,server.cpu
,server.memory
,server.hardDrive
FROM asset
LEFT JOIN server
    ON server.id = asset.id
WHERE asset.id = '$id'

I then assign all results into single php variables.

How would I write a query/script to update an asset?

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

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

发布评论

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

评论(2

惜醉颜 2024-09-09 20:49:25

由于资产和服务器之间(大概)存在一对多关系,因此您需要单独更新它们:

UPDATE assets SET
    company = '$company',
    location = '$location', ...
WHERE id = $asset_id;

UPDATE servers SET
    manufacturer = '$manufacturer',
    model = '$model', ....
WHERE $id = $server_id

您必须执行第二个查询 n 次,其中 n< /em> 是与特定资产关联的服务器记录数。

另外,我确实希望您小心对待这些变量 - 我们不希望这里有 Bobby Tables

更新:

好的,所以您可以一对一地放入多个表中。在这种情况下,为什么要使用 LEFT JOIN 呢? 服务器不可能还不是资产,对吗?无论如何,回答,从我的脑海中浮现出来:

UPDATE assets, servers
SET
    assets.company = ...,
    servers.manufacturer = ...
WHERE assets.id = $id
AND servers.id = $id

Since you have (presumably) a one-to-many relationship between assets and servers, you would need to update them separately:

UPDATE assets SET
    company = '$company',
    location = '$location', ...
WHERE id = $asset_id;

UPDATE servers SET
    manufacturer = '$manufacturer',
    model = '$model', ....
WHERE $id = $server_id

You will have to do the second query n times, where n is number of records of server associated with the particular asset.

Also, I do hope you're careful with those variables - we don't want Bobby Tables here.

UPDATE:

OK, so you have one-to-maybe-one into several tables. In that case why use LEFT JOIN at all? There shouldn't be a possibility a server is not already an asset, right? Anyway, answer, off the top of my head:

UPDATE assets, servers
SET
    assets.company = ...,
    servers.manufacturer = ...
WHERE assets.id = $id
AND servers.id = $id
软糯酥胸 2024-09-09 20:49:25

因为您的资产表和服务器表之间有 1-1 连接。它本质上是同一张表。如果您可以控制数据库布局,我会重新考虑。

如果没有,您将必须进行两次更新调用,每个表一次,就像 @Amadan 向您展示的那样

Since you have a 1-1 connection between your asset table and your server table. It is essentially the same table. If you have control over your database layout, I would rethink it.

If not you will have to do two update calls, one to each table like @Amadan showed you

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