问题
$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>
this is my php script. I have a 11 table email1, email2, email3, ... email11
join table and show duplicate email as 'yes' else 'no'
i want this output whats i am doing??
Output like this
回答1:
can you try this:
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
then you can loop by php:
while($row = mysql_fetch_array($result)) {
if($row['email_count']>1 ) {
echo 'yes';
} else {
echo 'no';
}
}
回答2:
Here is an example for 4 tables you can extend it till 11 if you need. Sorry, but I did not debug this code, I guess the main obstacle was the mysql query to get correct values from database.
And you definitely should stop using mysql*
functions!
$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>';
来源:https://stackoverflow.com/questions/34022662/how-to-display-duplicate-email-address