[英]PHP|MYSQL Query : compare two table fields and insert|overwrite value of one field if matches
I've got 2 tables :我有 2 张桌子:
jos_tempcsv
, which contains among others the following field: Category of goods
jos_tempcsv
,其中包含以下字段: Category of goods
jos_categories
, which contains among others the following fields: Category_id
, slug
jos_categories
,其中包含以下字段: Category_id
, slug
I would like to create the following query:我想创建以下查询:
I've got a table called jos_tempcsv
which contains a field called Category of goods
.我有一个名为
jos_tempcsv
的表,其中包含一个名为Category of goods
的字段。 This field contains alphanumeric values that needs to be changed in a numeric value.此字段包含需要更改为数值的字母数字值。
I need to compare the value of the Category of goods
field of the jos_tempcsv
table with the value of the slug
field of the jos_categories
table.我需要将
jos_tempcsv
表的Category of goods
字段的值与jos_tempcsv
表的slug
字段的值进行jos_categories
。
If comparison matches, then I would like to insert the value contained in the Category_id
field of the jos_categories
table into the Category of goods
field of the jos_tempcsv
table (it should overwrite the initial value with a numeric value).如果比较匹配,那么我想插入包含在价值
Category_id
基于场jos_categories
表到Category of goods
的领域jos_tempcsv
表(它应该覆盖与数值的初始值)。
Find a solution which works, record are updated|overwritten :找到一个有效的解决方案,记录被更新|覆盖:
$sql = "update jos_tempcsv u left join jos_virtuemart_categories_fr_fr c on c.slug=u.`Category of goods` set u.`Category of goods`=c.virtuemart_category_id;
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Thank you to @PRANAV and a special thanks to @Abdulla which has a great sens of humor感谢@PRANAV,特别感谢@Abdulla,他非常幽默
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.