如何查找相同列表中的电子邮件?

时间:2015-12-01 06:55:19

标签: php mysql mysqli left-join inner-join

我有11个表[email1,email2,email3,... email11]

<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("email-db", $con);
$sql = "SELECT Contact_Email FROM email1, email2, email3, email4, email5";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    ?>
    <tr>
        <td><? echo $row['Contact_Email']; ?></td>
        <td><? echo '<br>'; ?></td>
    </tr>
<? } ?>

我真正想要做的就是使用所有表中的联接来选择所有表中的所有电子邮件。怎么办呢?

3 个答案:

答案 0 :(得分:1)

MySQL UNION运算符允许您将多个表中的两个或多个结果集合并为一个结果集

SELECT Contact_Email FROM email1
UNION
SELECT Contact_Email FROM email2
UNION
SELECT Contact_Email FROM email3
.
.
UNION
SELECT Contact_Email FROM email11

默认情况下,即使您没有显式使用DISTINCT运算符,UNION运算符也会从结果中消除重复的行。

答案 1 :(得分:0)

$sql = "SELECT Contact_Email FROM email1 UNION ALL SELECT Contact_Email FROM email2 UNION ALL SELECT Contact_Email FROM email3 UNION ALL SELECT Contact_Email FROM email4 UNION ALL SELECT Contact_Email FROM email5 UNION ALL SELECT Contact_Email FROM email6 UNION ALL SELECT Contact_Email FROM email7 UNION ALL SELECT Contact_Email FROM email8 UNION ALL SELECT Contact_Email FROM email9 UNION ALL SELECT Contact_Email FROM email10 UNION ALL SELECT Contact_Email FROM email11";  

我用过这个

答案 2 :(得分:0)

自从社会投票结束重复how to display duplicate email address

以下是4个表的示例,如果需要,可以将其扩展到11个。对不起,但是我没有调试这段代码,我猜主要的障碍是从数据库中获取正确值的mysql查询。

你绝对应该停止使用mysql *函数!

$sql ="SELECT t.Contact_Email,
  e1.Contact_Email email1,
  e2.Contact_Email email2,
  e3.Contact_Email email3,
  e4.Contact_Email email4
FROM (
SELECT e.Contact_Email FROM email1 e
UNION ALL 
SELECT e.Contact_Email FROM email2 e
UNION ALL 
SELECT e.Contact_Email FROM email3 e
UNION ALL 
SELECT e.Contact_Email FROM email4 e
) t
LEFT JOIN email1 e1
ON t.Contact_Email = e1.Contact_Email
LEFT JOIN email2 e2
ON t.Contact_Email = e2.Contact_Email
LEFT JOIN email3 e3
ON t.Contact_Email = e3.Contact_Email
LEFT JOIN email4 e4
ON t.Contact_Email = e4.Contact_Email";

echo '<table><thead><tr><th>Email</th>';
for ($i=1;$i<5; $i++){
    echo "<th>email $i</th>";
}
echo '</tr></thead><tbody>';

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    echo '<tr><td>'.$row['Contact_Email'].'</td>';
    for ($i=1;$i<5; $i++){
        echo '<td>'.(empty($row['email'.$i])?'no':'yes').'</td>';
    }
    echo '</tr>';
}
echo '</tbody></table>';

更新使用相同的查询,但将输出部分更改为:

echo '<table><thead><tr><th>Email</th>';
for ($i=1;$i<5; $i++){
    echo "<th>email $i</th>";
}
echo "<th>Total (yes)</th>";
echo "<th>Total (no)</th>";
echo '</tr></thead><tbody>';

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $yesCount = 0;
    $noCount = 0;
    echo '<tr><td>'.$row['Contact_Email'].'</td>';
    for ($i=1;$i<5; $i++){
        echo '<td>'.(empty($row['email'.$i])?'no':'yes').'</td>';
        if (empty($row['email'.$i])) {
            $noCount++;
        } else {
            $yesCount++;
        }
    }
    echo '<th>'.$yesCount.'</th>';
    echo '<th>'.$noCount.'</th>';
    echo '</tr>';
}
echo '</tbody></table>';