I am new to MYSQL and have a problem. Its related to php too I guess.
I have a table,
ID,CODE,string1,string2,string3,string4,string5,string6,string7
now, each customer has a unique code. Each code can store a maximum of 7 strings in their “list”.
PROBLEM – if the customer adds a new item or items (important!) to their list and their list is already full, then the list should push out the oldest string and add the new strings to the end.
So say, string 1 to 7 are filled with text t1 to t7.
then customer wants to add new items t8 and t9, I need to delete t1 and t2, move everything down 2 spaces and add t8 where t6 was and t9 where t7 was (at the beginning). This is easy to do in java or normal programming, but how can I do this quickly and easily and efficiently in mysql/php code?
Is it possible? Is this the correct way to do things in mysql? Is there a better way? sorry
I think your problem is more related to your database schema.
You could change (if you can) your db like this :
Then, for your user (1, ‘xxx’), you add 7 values :
And, following your description, if you add two new numbers (8, 9) :
and then add the two new entries :
Now, suppose you have three customer :
The difference would occur in the user_id column in the List table :
(I reduce to 2 entry per user) :
Now suppose you want to have the list of the user 3, you do this request :
This will return two rows : 1, 2
You don’t have to create a table per user, you just have to add an extra column to identify which user belongs the data in the same table than others users.
Hope this helps ! 🙂