Situation. We have 3 tables. User, add. user emails and some data associated with each email.
- table_users
UserID | username | email
- table_emails
UserID | additional_email
- table_data
email | date_added | email_data
TASK: username | email_data
For each user, you need to display some data (email_data), the first in time to add (date_added). Moreover, the data can be tied to any of the user's emails, both to the main one from the table table_users, and to the additional email from table_emails, which can be infinitely many (really, of course, 2-3 pieces). Data email_data for each email can be several, and maybe not at all, so something like this: LEFT JOIN ..ORDER BY date_added LIMIT 1. All this is in conjunction with php, so I think somehow dynamically build a query, in the first step, collect all emails into an array, and then type:
<?php foreach( $emails as $email ) { $i++; $sql .= " LEFT JOIN table_data as td$i ON td$i.email = $email"; } ?> But somehow, the final request is still not invented ... Help plz.
Request final need a single. I can not, by various requests, first pull out all the data for one email, then if there is an additional one, etc., and glue them together on php. Because there everything is more complicated really, all tables have more fields and by them it is necessary to filter, sort and paginate.