Skip to content

Instantly share code, notes, and snippets.

@gabrieldarezzo
Created March 14, 2019 22:33
Show Gist options
  • Save gabrieldarezzo/60c1f68b3fb2fbd91776c6aa612c3608 to your computer and use it in GitHub Desktop.
Save gabrieldarezzo/60c1f68b3fb2fbd91776c6aa612c3608 to your computer and use it in GitHub Desktop.
```sql
CREATE TABLE products (
product_id INT(11) PRIMARY KEY AUTO_INCREMENT,
product_desc VARCHAR(250) NOT NULL UNIQUE,
imagesrc VARCHAR(250)
);
SELECT
CONCAT('INSERT INTO products (product_id, product_desc) VALUES (', @rownum, ',\'', product_id, '\');') AS sql_stmt,
CONCAT('UPDATE local set product_id = ', @rownum, ' WHERE product_id = \'', product_id, '\';') AS sql_stmt,
product_id,
@rownum := @rownum + 1
FROM
(
SELECT
DISTINCT product_id
FROM
LOCAL
WHERE product_id IS NOT NULL
AND product_id <> ''
) AS tbl
,(SELECT @rownum := 1) r;
ALTER TABLE `local` CHANGE `product_id` `product_id` INT(11) NOT NULL;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment