I wanted to further elaborate on a question being posed here- Insert and set value with max()+1 problems
Say we start with the same scenario SOLUTION:
INSERT INTO
customers( customer_id, firstname, surname )
SELECT MAX( customer_id ) + 1, 'jim', 'sock' FROM customers
Let's also say that the whole Sock family are customers at this shop. For the sake of the question, let's say there are 10 unique firstname
's all with their own unique customer_id
's as well, BUT they all have the same surname 'sock'
.
What I want to do is say Big Papa Sock comes in and wants to replicate his whole family under his new last name ('shoe'
), and for each entry we need a new unique customer_id
(that is +1 of the highest current customer_id
).
So the data we currently have in a mock customers
table is-
customer_id firstname surname
1 jim sock
2 sue sock
3 brad sock
4 tony sock
5 paul sock
6 betty sock
7 allen sock
8 rita sock
9 pam sock
10 deb sock
And I want to insert the new 'shoe'
data to get a new customers
table-
customer_id firstname surname
1 jim sock
2 sue sock
3 brad sock
4 tony sock
5 paul sock
6 betty sock
7 allen sock
8 rita sock
9 pam sock
10 deb sock
11 jim shoe
12 sue shoe
13 brad shoe
14 tony shoe
15 paul shoe
16 betty shoe
17 allen shoe
18 rita shoe
19 pam shoe
20 deb shoe
So essentially what I need it to do is replicate each combination of unique entries and insert into customers. So it should keep the firstname
the same, but add a new entry based on the max customer_id
+1 and the new surname
, 'shoe'
What I have tried so far is-
INSERT INTO
customers(customer_id, firstname, surname)
SELECT MAX(customer_id) + 1, firstname, 'shoe' FROM customers
WHERE surname = 'sock'
But there seems to be an error when inserting.
Any help would be greatly appreciated.