如何显示重复的电子邮件地址

时间:2015-12-01 14:21:06

标签: php mysql join duplicates

$sql ="SELECT u.Contact_Email FROM email1 AS u UNION ALL SELECT e.Contact_Email FROM email2 AS e"; $result = mysql_query($sql);
 $yes = 'yes';
 $no = 'no';
 echo "<table><tr><th>Email</th><th>email 1 opened</th><th>email 1 clicked</th><th>email2 opened</th><th>email2 clicked</th><th>email3 opened</th><th>email4 opened</th><th>email 4 clicked</th><th>email 5 opened</th><th>email 5 clicked</th><th>email 6 opened</th><th>email 6 clicked</th></tr><tbody>";
 while($row = mysql_fetch_array($result)) {
            $d_name = $row['Contact_Email'];
    ?> 
          <tr>
            <td><?php echo $d_name; ?></td>
            <?php if($row['Contact_Email']  == $row['Contact_Email']){
            echo "<td>yes</td>";
            } else if ($row['Contact_Email']  == 0){
            echo "<td>no</td>";
            }    ?>
            <td><?php echo $yes; ?></td>
          </tr>
          <?php } ?>
           </tbody>
    </table>

这是我的PHP脚本。我有一个11表email1,email2,email3,... email11
加入表并将重复的电子邮件显示为“是”否则为“否” 我想要这个输出我正在做什么? Output like this

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

select count(email) as email_count, email from ( 
select email_1 as email  from email 
union all select email_2 as email  from email_2 
union all select email_3 as email  from email_3
union all select email_4 as email  from email_4
.......
) as emails_table group by emails_table.email

然后你可以通过php循环:

while($row = mysql_fetch_array($result)) {
if($row['email_count']>1 ) {
echo 'yes';
} else {
echo 'no';
}
}

答案 1 :(得分:0)

以下是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>';