Answer the question
In order to leave comments, you need to log in
How to combine data in MySQL tables by non-unique features?
There are two tables in the MySQL database (5.5) of the following form: id (primary, int) - phone (varchar) - added (date) - userid (int
) correlate, in each of the tables id is unique only for it), in the second - only phone and added. It is necessary to transfer the data of the userid column from the first table to the second by matching phone-added pairs, but taking into account the fact that on the same day (the same value added) and for the same number (the same value phone) there may be different user_id .
Without the last condition, the query is simple:
UPDATE table2 AS t2
LEFT JOIN table1 AS t1 ON t2.phone = t1.phone AND t2.added = t1.added
SET t2.userid = t1.userid
WHERE t1.phone = t2.phone AND t1.added = t2.added
Answer the question
In order to leave comments, you need to log in
Well ... as an option, you can add an exported field to the table2 table, which indicates that this row has already been transferred.
The algorithm is as follows:
We look for a row in table2 with the desired phone, the desired date and exported=0
If we find it, we transfer the user_id and set exported=1
If we don’t find it, we transfer the user_id from any line that has a suitable phone and date and set exported=1
Maybe it's worth normalizing the database structure and moving phone and added into a separate entity common to 2 tables?
Well, there is nothing special to think about, if I understood your request correctly.
select min(User_id) from your table group by Phone, Added -- this will get the first value inserted by this phone and date.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question