我如何使用 SQL 和 PHP 更新这些表条目?
我正在解决资产数据库问题。
我将资产输入数据库。每个对象都是资产,并且在资产表中具有变量。对象也是一种资产。在此示例中,类型为服务器。
这是检索所有必要数据的查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于资产和服务器之间(大概)存在一对多关系,因此您需要单独更新它们:
您必须执行第二个查询 n 次,其中 n< /em> 是与特定
资产
关联的服务器
记录数。另外,我确实希望您小心对待这些变量 - 我们不希望这里有 Bobby Tables。
更新:
好的,所以您可以一对一地放入多个表中。在这种情况下,为什么要使用 LEFT JOIN 呢?
服务器
不可能还不是资产
,对吗?无论如何,回答,从我的脑海中浮现出来:Since you have (presumably) a one-to-many relationship between assets and servers, you would need to update them separately:
You will have to do the second query n times, where n is number of records of
server
associated with the particularasset
.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 aserver
is not already anasset
, right? Anyway, answer, off the top of my head:因为您的资产表和服务器表之间有 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