Ayuda de actualización de mysql necesaria para agregar prefijo a las entradas

Sorry for the poor title, but I couldn't think of another way to describe it. What I need to do is, add a prefix to certain entries in a column.

For example, a column could have number entries such as: 92209.1,92201,1,92202,1 etc. I need to add a prefix to only numbers without the prefix. 92209.1 becomes TMP92201,1.

I know how to use the UPDATE function, but not sure how to use with this type of query. I would be grateful for any help that you can offer. Thanks

preguntado el 08 de noviembre de 11 a las 11:11

Please do the following: edit your post to add some line feeds there to make it readable; provide an example of what you would want to do. -

Is this a one-time fix and therefore performance isn't much of a problem (i.e. it doesn't matter whether it take 0.1, 1 or 10 seconds)? -

@VolkerK 1 time. So time isn't an issue. Thanks -

@Romain. I have given an example in my question. Thanks -

Then please fix the typo(s) in the example: "92209.1 becomes TMP02201,1" -

2 Respuestas

You should use a statement along the lines of:

UPDATE TABLE_NAME SET FIELD_NAME = CONCAT('TMP', RIGHT(FIELD_NAME, LENGTH(FIELD_NAME) - 7)) WHERE FIELD_NAME LIKE '92209.1%'

Essentially, it'll change the value of the field named FIELD_NAME from the table called TABLE_NAME for every row that has a FIELD_NAME empezando con '92209.1'. The value that will be set is obtained by concatenating 'TMP' y el valor de FIELD_NAME to which we removed the left portion of the size of the prefix (7 characters).

respondido 08 nov., 11:15

Romain. Could you explain this statement please. Thanks - bollo

Added explanation in the answer. - romano

Unfortunately, not all the entries are 7 characters. So, would it be just a case of doing the 7 digit series first then the 6 digit series. etc. thanks - bollo

También puedes hacer el 7 parte del RIGHT dynamic (you could use LENGTH(PREFIX) there) if you have the set of prefixes somewhere in a table/view (or can otherwise make it available in a table-ish form). - romano

Let's assume the table

CREATE TEMPORARY TABLE soFoo (
    id int auto_increment,
    v varchar(32),
    primary key(id)
)

You can select all rows having a v without the prefix via

SELECT
  *
FROM
  soFoo
WHERE
  v NOT LIKE 'TMP%'

and you can limit an update query with the same WHERE clause to only affect rows having a v without the prefix.

UPDATE
  soFoo
SET
  ...
WHERE
  v NOT LIKE 'TMP%'

Now you want to set those vs to their current value prefixed with your static string literal

SET
  v = CONCAT('TMP', v)

complete query:

UPDATE
  soFoo
SET
  v = CONCAT('TMP', v)
WHERE
  v NOT LIKE 'TMP%'

ejemplo autónomo:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

echo "before:\n";
foreach( $pdo->query("SELECT * FROM soFoo", PDO::FETCH_ASSOC) as $row ) {
    echo join(' | ', $row), "\n";
}

$query = "
    UPDATE
        soFoo
    SET
        v = CONCAT('TMP', v)
    WHERE
        v NOT LIKE 'TMP%'
";
$pdo->exec($query);

echo "\n\nafter:\n";
foreach( $pdo->query("SELECT * FROM soFoo", PDO::FETCH_ASSOC) as $row ) {
    echo join(' | ', $row), "\n";
}

function setup($pdo) {
    $pdo->exec('CREATE TEMPORARY TABLE soFoo (
        id int auto_increment,
        v varchar(32),
        primary key(id)
    )');

    $stmt = $pdo->prepare('INSERT INTO soFoo (v) VALUES (:v)');
    $stmt->bindParam(':v', $v);
    // add some rows with and some without the prefix
    for($i=0; $i<20; $i++) {
        $v = rand(1000, 10000);
        if ( 0==$v%2 ) {
            $v = 'TMP'.$v;
        }
        $stmt->execute();
    }
}

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.