Jump to content

Creating a column which increments based on the value of another column


rod985

Recommended Posts

Hi All,

I have a "customers" table with the following columns: "id", "owner_id", "customer" with a lot of rows already in it.

Now, I want to add a column called "owner_customer_id". This column would be a customer ID that's unique to the store owner, but not unique to the table (in other words, "owner_id" PLUS "owner_customer_id" is unique)

 

When I add "owner_customer_id" to the table, all the values are initially set to 0. I would like to properly populate the values *without* bringing PHP into the picture - a pure SQL update. Here's an example of what data should look like afterwords:

 

id, owner_id, owner_customer_id, customer

1, 1, 1, Customer A

2, 1, 2, Customer B

3, 1, 3, Customer C

4, 2, 1, Customer A

5, 2, 2, Customer B

6, 1, 4 Customer D

 

What would be the best approach to accomplish this?  Thanks in advance!

 

- Anthony

Link to comment
Share on other sites

This is a shared database. Multiple ecommerce stores use the same tables. So for example, when a store owner exports a list of customers, I do not want them seeing the system-level ID, but rather a properly-incremented ID that is specific to the store scope.

Link to comment
Share on other sites

They are completely separate.

Store Owner A has customers

Store Owner B has customers

Store Owner A share absolutely no connection to Store Owner B except that they share a database table on a scaled system.

 

It would be strange for Store Owner A to see the system ID when exporting customers. Say he does an export. He will get back:

1, 1, 1, Customer A

2, 1, 2, Customer B

3, 1, 3, Customer C

6, 1, 4 Customer D

... Naturally, the question will be - why are 4 & 5 missing. The answer, of course, is that 4 & 5 belong to Store Owner B. But that's unacceptable. Store Owner A shouldn't see a skip in Customer IDs just because he shares the database with someone else. Hence, owner_customer_id - an ID that is unique to the individual store owner

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.