Giraffeknees
Giraffeknees

Reputation: 1

How to fill 2 separate columns with only the most recent data associated with data

In a database that I'm creating, I need to match parent contact information with kids that are given to me in a list format. I'm matching student ID numbers with parent emails as primary keys and using a merger table to create a many to many relationship. In order to upload info to a 3rd-party website, I want the two most recently updated parent emails to show up in separate columns if there is more than one for the same kid. I can get a value into one column easy enough, but getting the second and making sure that it is the most recently updated value is too much for me.

I'm still very new to SQL, so if my code is unconventional, that's why.

UPDATE namelist
INNER JOIN `student-parent` ON namelist.ID = `student-parent`.ID
INNER JOIN parent ON `student-parent`.email = parent.email
SET namelist.email1 = `student-parent`.email;

Is there a way that I can use either SORT BY or GROUP BY and maybe use those values to get things into separate places? That is to say, to not only populate the email1 column, but also the email2 column if there are two or more emails associated with that student?

Am I able to do an UPDATE INNER JOIN HAVING COUNT(email) > 1; statement and somehow designate the first and second value or something like that? Maybe some clever CONCAT trick? I've never used CASE expressions, but maybe this is the place for them?

Upvotes: 0

Views: 26

Answers (0)

Related Questions