Search code examples
phpmysqljoininner-join

Fatch which emails are on the same lists different tables but same column name email addressess


I have a 11 tables

email1,email2,email3,email4,email5,email6,email7,email8,email9,email10,email11

and same column name Contact_Email with different email address

<?php
    $con = mysql_connect("localhost","root","");
    $db = mysql_select_db("email-db",$con);

    $sql = "SELECT Contact_Email FROM email1,email2,email3,email4";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
    ?>
    <tr>
      <td><? echo $row['Contact_Email']; ?></td>
      <td><? echo '<br>'; ?></td>
    </tr>
    <? } ?>

actually I want this output

select all emails from all tables using join foreach emails as email check if email is in table 1, if yes put yes in td check if email is in table 2, if yes put yes in td check if email is in table 3, if yes put yes in td

etc

kindly help me what can I do?


Solution

  • Just use UNION, following this example:

    SELECT u.email
    FROM email1 AS u
    UNION 
    SELECT e.email
    FROM email2 AS e
    

    In PHP:

    $sql = "SELECT u.email FROM email1 AS u 
    UNION SELECT e.email FROM email2 AS e";
    $result = mysql_query($sql);while($row = mysql_fetch_array($result))
     { ...}
    

    To avoid MYSQL ERROR "Illegal mix of collations for operation 'UNION'" Please synchronize all email fields to be with equal parameters