I have these two tables:
desc students
+-----------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------+------+-----+---------+----------------+
| student_id | int(11) | NO | PRI | NULL | auto_increment |
| student_ticket_number | int(11) | YES | | 0 | |
+-----------------------+---------+------+-----+---------+----------------+
desc studentdates
+-----------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------+------+-----+---------+----------------+
| student_date_id | int(11) | NO | PRI | NULL | auto_increment |
| student_id | int(11) | YES | | NULL | |
| student_ticket_number | int(11) | YES | | 0 | |
+-----------------------+---------+------+-----+---------+----------------+
I would like to move the column students.student_ticket_number to studentdates.student_ticket_number where the field student_id match.
So if the user John has student_id = 1 move his ticketnumber (for example 1234) from students.student_ticket_number to studentdates.student_ticket_number WHERE student_id = '1'.
In the table studentdates.student_id there can be multiple identical records then I would like to use the lowest studentdates.student_date_id and skip the others. Tell me if this is unclear.
I guess I need to do a subquery somehow but how?
You need to get the minimum
student_date_IDin a subquery then join it with the other tables. try this,