简体   繁体   中英

SQL update to auto-increment column

I am creating a test database. I have a bunch of columns and I want to 'purge' the sensitive information so that I can work on it.

id   email
1    email1@no-reply.com
2    email2@no-reply.com
#    email#@no-reply.com

basicall I want do do UPDATE table SET email = "email" + id + "@no-reply.com

I was going to do this in Python, but thought doing it in SQL would be easier.

For your case, it'll be:

UPDATE table
SET email = CONCAT( "email", id, "@no-reply.com" );

I just used it on my own table late of mysql. Here is the result:

mysql> update late
    -> set msg = concat( id, " ", msg );
Query OK, 74397 rows affected (4.15 sec)
Rows matched: 74397  Changed: 74397  Warnings: 0

Yes, if you are using update you should already have the id. If it was php it would look like:

$query = 'Update email set email="email'.$id.'@no-reply.com where email.id='.$id;

Where $query is the query string you are building and $id is the record id you are using to reference the record.

UPDATE table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

For the single-table syntax, the UPDATE statement updates columns of existing rows in the named table with new values. The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value. The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated. If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM