简体   繁体   English

PHP|MYSQL 查询:比较两个表字段,如果匹配,则插入|覆盖一个字段的值

[英]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_idslug

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.

相关问题 通过php将一个表中的一个字段与mysql中花药表中的几个字段进行比较 - Compare one field from one table to several fields in anther table in mysql via php 如何将php中的两个字段与mysql中的不同表进行比较? - how to compare the two fields in php with different table in mysql? 如何在一个表中比较两个具有相同值但效果不同的字段? - how to compare two field with same value with different effect in one table in mysql? PHP查询在MySQL表中插入“ 0”值 - PHP query insert '0' value to mysql table PHP / MYSQL:比较一列中的单元格,并覆盖另一列或表中的匹配子字符串 - PHP/MYSQL: Compare cells in one column and overwrite matching substrings in another column or table 在php mysql查询中检查两个表的值 - Check two table value in php mysql query jQuery使用PHP和MySQL基于一个字段自动完成两个字段 - Jquery autocomplete two fields based on one field using PHP and MySQL MYSQL连接具有相同字段值的两个表字段,但如果另一个字段与另一个字段不匹配,则仍将包括 - MYSQL Join two table fields with same field value but will still include if the other field doesn't match the other one 比较两个表,从表中获取价值并将其相乘-PHP MySQL - Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL PHP MySQL如何在一个表行中关联两个字段? - PHP MySQL how to associate two fields in one table row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM