Advertisement
Guest User

INSERT … SELECT demo for http://stackoverflow.com/q/20603197

a guest
Dec 17th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. palec@Palec:~$ mysql
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4.  
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 134
  7. Server version: 5.1.49-3~bpo50+1 (Debian)
  8.  
  9. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
  10. This software comes with ABSOLUTELY NO WARRANTY. This is free software,
  11. and you are welcome to modify and redistribute it under the GPL v2 license
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15. mysql> CREATE TABLE clients (clientNum VARCHAR(11) NOT NULL, name VARCHAR(20) NOT NULL, PRIMARY KEY (clientNum));
  16. Query OK, 0 rows affected (0.00 sec)
  17.  
  18. mysql> INSERT INTO clients VALUES('000-100-000', 'A'),('000-000-100', 'B'),('500-000-000', 'C');
  19. Query OK, 3 rows affected (0.00 sec)
  20. Records: 3 Duplicates: 0 Warnings: 0
  21.  
  22. mysql> INSERT INTO clients SELECT INSERT(INSERT(LPAD(CAST(COALESCE(MAX(CAST(REPLACE(clientNum, '-', '') AS UNSIGNED)), 0) + 1 AS CHAR), 9, '0'), 7, 0, '-'), 4, 0, '-'),'John Doe' FROM clients WHERE clientNum BETWEEN '000-100-000' AND '000-199-999';
  23. Query OK, 1 row affected (0.00 sec)
  24. Records: 1 Duplicates: 0 Warnings: 0
  25.  
  26. mysql> SELECT * FROM clients;
  27. +-------------+----------+
  28. | clientNum | name |
  29. +-------------+----------+
  30. | 000-100-000 | A |
  31. | 000-000-100 | B |
  32. | 500-000-000 | C |
  33. | 000-100-001 | John Doe |
  34. +-------------+----------+
  35. 4 rows in set (0.01 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement