Mysql结果

时间:2018-02-19 13:06:20

标签: php mysql

我需要从MySQL表中获取一个值,然后用其他东西替换它。

我有一个查找和替换表,它有三列:ID,查找,替换。我想循环遍历该表的每一行,然后在另一个表中搜索查找值并将其更改为替换。

例如:

ID | Find                   | Replace
1  | This is a product name | Product Name 

我创建了一个遍历每一行的循环,然后在另一行内部循环遍历表我将替换值:

$lookups = mysqli_query($connect, "SELECT * FROM value_lookup");

while($row = mysqli_fetch_assoc($lookups)) {
    $find = $row['Find'];
    $replace = $row['Replace'];

    $table = mysqli_query($connect, "SELECT ID, Reference FROM table");

    while($row = mysqli_fetch_assoc($table)) {
        $ref = $row['Reference'];
        $newRef = str_ireplace($find, $replace, $ref);

        echo $newRef . PHP_EOL;
        mysqli_query($connect, "UPDATE table SET Reference = '$newRef' WHERE Reference LIKE '%$find%'");
    }
}

在我看来,str_ireplace不起作用,因为我使用的值是一个关联数组。如果我手动键入值作为字符串,它工作正常。

我需要知道如何将值作为字符串获取,或将它们转换为一个。我也必须使用str_ireplace,我不能直接获取find值并更改为replace。由于某些值具有不同的前缀和后缀,我希望保持不变:

  

这些是一些毛巾:红色毛巾:红色   
这些是毛巾:蓝色毛巾:蓝色

2 个答案:

答案 0 :(得分:1)

在为mickmackusa创建一个SQLfiddle时,我实际上已经设法使用纯SQL为自己工作了。

DELIMITER //
CREATE PROCEDURE findreplace()
BEGIN   
DECLARE cnt INT DEFAULT 0;
DECLARE total INT DEFAULT 0;
DECLARE original VARCHAR(255);
DECLARE new VARCHAR(255);

SELECT COUNT(*) FROM lookup INTO total;

WHILE cnt < total DO
    SELECT find FROM lookup LIMIT cnt, 1 INTO original;
    SELECT repl FROM lookup LIMIT cnt, 1 INTO new;

    UPDATE test SET Reference = replace(Reference, original, new);
    SET cnt = cnt + 1;
END WHILE;
END//

答案 1 :(得分:0)

您可以使用单个更新查询来执行此操作。您可以在WHERE子句中使用REGEXP OR LIKE运算符来查找相应的&amp;替换以下是查询:

使用 REGEXP

UPDATE table, findreplacetable SET table.name = REPLACE(table.name, findreplacetable.find_, findreplacetable.replace_) WHERE table.name LIKE CONCAT('%', findreplacetable.find_,'%');

使用 LIKE

{
  "name": "myproject",
  "version": "4.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "prestart": "node-sass -r src/styles/themes -o temp/themes",
    "start": "ng serve --ec=true",
    "build": "ng build --prod",
    "postbuild": "node-sass -r src/styles/themes -o dist/themes",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "^4.0.2",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.7",
    "@ngx-translate/core": "^6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "angular2-toaster": "^3.0.1",
    "c3": "^0.4.11",
    "chart.js": "^2.5.0",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "fullcalendar": "^3.4.0",
    "jquery": "^2.1.0",
    "ng2-charts": "^1.5.0",
    "ng2-date-picker": "^1.4.2",
    "ng2-smart-table": "^1.1.0",
    "ngx-progressbar": "^2.0.0",
    "perfect-scrollbar": "^0.7.0",
    "rxjs": "^5.1.0",
    "zone.js": "0.8.5",
    "angular2-jwt":"0.2.0",
    "ngx-loading" :"1.0.14 ",
    "ng2-auto-complete" :"0.12.0",
    "@ngui/tab" : "0.5.0",
    "@ngui/datetime-picker" : "0.16.2",
    "ngx-modal" :"0.0.29"



  },
  "devDependencies": {

    "@types/c3": "^0.4.41",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "node-sass": "^4.5.3",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0"
  }
}

希望它会帮助你。