Jump to content

Merging multiple rows


arbitter

Recommended Posts

My table 'guess':

 

id (int) | user_id (int) | logo_id (int) | guess (varchar) | guess_count (int) | guessed (int)

id is unique,

unique relation between user_id & logo_id

 

When a user visits the site, a user is made in a 'users' table (so there's a new user_id). The user can then play with this user_id. When the user logs in, the current scores/guesses/... should be transferred to the user_id of the logged in-account.

 

How do I get this done?

 

To clarify: there are two user_id's in play - the user_id from the registered (logged in) user, and the user_id from the session (not logged in). I will refer to these as user_id_login and user_id_session.

 

So, if a row of user_id_login has a guessed = 1 (this int is 0/1. 1 means guessed, 0 means not guessed) for certain logo_id, a row with that logo_id and the user_id_session should be completely ignored.

If a row of user_id_login = 0 for certain logo_id exists, the values 'guess_count', 'guessed' and 'guess' of the row with the same logo_id and with user_id_session should be added (for guess_count) or stored (guessed & guess).

If a row with user_id_session for certain logo_id exists and hasn't been checked in one of the previous, the user_id of this should be changed from user_id_session to user_id_login.

 

Now how I currently would think about doing this, is check the table for the user_id_login where guessed = 1. For each result, then do a query to check if there is a user_id_session with that logo_id and remove them.

Next, check the table for the user_id_login with guessed = 0. For each result, then check if there is a user_id_session with the same logo_id and add those values, then delete that row.

And at last, a query to check all the leftover user_id_session and change the id to user_id_login.

 

As you can see, this is dozens of query's, so there must be a better way

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.