Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从表1中获取多个值,将它们更改为基于表2的值,并将结果写入表1_Php_Mysql - Fatal编程技术网

Php 从表1中获取多个值,将它们更改为基于表2的值,并将结果写入表1

Php 从表1中获取多个值,将它们更改为基于表2的值,并将结果写入表1,php,mysql,Php,Mysql,你好 我可以基于其他列的值在mysql数据库中重写列的值,但这次我需要做一些调整… 有两个表(同时有多个值):规格和产品 产品有两列:specificationids和specificationorderids 规范也有两列:id和specificationorder specificationids有如下格式的多个值:,31,29,27,18, 这些也是规格表中的id。每个id都有一个specificationorder值(同一行,其他列)。现在我想做的是,我想用specificationor

你好
我可以基于其他列的值在mysql数据库中重写列的值,但这次我需要做一些调整…
有两个表(同时有多个值):规格产品

产品有两列:
specificationids
specificationorderids

规范也有两列:
id
specificationorder

specificationids
有如下格式的多个值:,31,29,27,18,
这些也是规格表中的
id
。每个
id
都有一个
specificationorder
值(同一行,其他列)。现在我想做的是,我想用
specificationorder
的值交换
id
的值,并将它们以相同的格式写入products表中的
specificationorderids

当然,这个过程必须循环通过产品表中的所有id

我希望我把问题说清楚了,可以理解


谢谢你的帮助

交换id规范订单规范订单规范订单id中写入,如果我了解您:

<?php
/*  Swap id and specificationorder */
$sql = "SELECT id, specificationorder from specifications";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
$id = $row[0];
$sporder = $row[1];
$sql = "UPDATE specifications SET id=$id, specificationorder=$sporder WHERE id=$id";
mysql_query($sql);
}

/* specificationorder writen in specificationorderids */
$sql = "SELECT specificationorder from specifications";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
$sporder = $row[0];
$sql = "INSERT INTO products(specificationorderids) VALUES($sporder)";
mysql_query($sql);
}
?>

在过去的几天里,我一直没能想出一个正常的代码,所以这里是我到目前为止得到的。我添加了注释,所以你知道我在试验代码时的想法。。。在我之前的评论中,我加入了一幅我想要实现的图像:img822.imageshack.us/i/specm.png

//get specificationids 
$sql = "SELECT specificationids from products";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$spids = $row[0];

//get specifications
$sql2 = "SELECT id from specifications";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_row($result2);
$spid = $row2[0];

//get specificationorder
$sql3 = "SELECT specificationorder from specifications";
$result3 = mysql_query($sql3);
$row3 = mysql_fetch_row($result3);
$sporder = $row3[0];

//if the value of specificationids matches a specificationid, then....
while(VALUES($spids)==$spid)
{
$spids=$sporder; //...it should be replaced by the specificationorder
//and then update the specificationorderids column accordingly
$sql = "UPDATE products(specificationorderids) VALUES($spids)";
mysql_query($sql);
}

规范ID
有如下格式的多个值:
、31,29,27,18、
。哦,孩子,在继续这条路之前,你真的需要阅读关于规范化的内容。同意@ypercube。正因为如此,您的查询才变得复杂。考虑一对多关系表。嗨。谢谢你的代码,但它仍然不完全是我想要的。现在,此代码将specificationorders插入到product表的新行中。我把我真正的意思描绘了出来。我发现用这种方式来描述问题更容易,只是为了避免将来的音乐理解:)这就是图像。给出的是规格表中的“id”和“规格订单”以及产品表中的“规格id”。因此,从这三个组合中,我想填写“SpecificationOrderID”(标记在图像上)。有什么建议吗?我邀请您重新设计您的数据库模型,您将来会遇到严重的问题。