我目前有2个表格,如下所示:
这两个表的代码如下所示:
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">Mail_ID </div></th>
<th width="91"> <div align="center">Status </div></th>
<th width="91"> <div align="center">Datum </div></th>
</tr>
<?php
$j = 0;
while($objResult2 = mysql_fetch_array($objQuery2))
{
$j++;
?>
<tr>
<td><div align="center"><?=$objResult2["Mail_ID"];?> </div></td>
<td><div align="center"><?=$objResult2["Status"];?> </div></td>
<td><div align="center"><?=$objResult2["Datum"];?> </div></td>
</tr>
<?php
}
?>
</table>
<br/>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">ID </div></th>
<th width="91"> <div align="center">Titel </div></th>
<th width="91"> <div align="center">Subscribe </div></th>
<th width="91"> <div align="center">Unsubscribe </div></th>
</tr>
<?php for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) : ?>
<tr>
<td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>"> </div></td>
<td><div align="center"><?=$objResult1["Titel"];?> </div></td>
<td><div align="center"><input type="radio" name="sub[<?php echo $i; ?>]" value="10"> </div></td>
<td><div align="center"><input type="radio" name="sub[<?php echo $i; ?>]" value="90"> </div></td>
</tr>
<?php endfor; ?>
</table>
查询和变量如下所示:
$strSQL1 = "SELECT ID, Titel FROM Mail";
$strSQL2 = "SELECT * FROM Subscriptions,
(SELECT MAX(ID) as ids, Mail_ID FROM Subscriptions
WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID) table2
WHERE ID=table2.ids";
$objQuery1 = mysql_query($strSQL1);
$objQuery2 = mysql_query($strSQL2);
现在我想将这两个表合并为:
我无法让这个工作,因为我尝试的所有内容由于其中的2个循环而混合了一切。我希望你们理解我的问题,我添加了尽可能多的信息和图片,以使一切都清楚^^如果你还有问题,只要问他们评论。欢迎任何帮助!
答案 0 :(得分:0)
我不确定这是否适合您,但它应该是一个开始:
SELECT Mail.ID Mail_ID, Mail.Titel, Subscriptions.status, Subscriptions.Datum, Subscriptions.Subscribe, Subscriptions.Unsubscribe
FROM Mail left join Subscriptions on Mail.ID = Subscriptions.Mail_ID
答案 1 :(得分:0)
使用SQL Join形成一个返回所有数据的SQL查询。然后只使用一个循环。
SQL可能类似于:
SELECT ID, Titel, Subscriptions.* FROM Mail INNER JOIN Subscriptions ON Subscriptions.ID = Mail.ID ...
答案 2 :(得分:0)
你应该像其他人已经说过的那样使用联接。通过连接两个或多个表您不需要使用多个while循环,代码将更加清洁
SELECT
Mail.ID Mail_ID,
Mail.Titel,
Subscriptions.status,
Subscriptions.Datum,
Subscriptions.Subscribe,
Subscriptions.Unsubscribe
FROM
Mail
LEFT JOIN Subscriptions
ON (Subscriptions.Mail_ID = Mail.ID)
其中一种连接类型适用于您:LEFT JOIN或INNER JOIN。
HTML应如下所示:
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">Mail_ID </div></th>
<th width="91"> <div align="center">Status </div></th>
<th width="91"> <div align="center">Datum </div></th>
<th width="91"> <div align="center">Titel </div></th>
<th width="91"> <div align="center">Subscribe </div></th>
<th width="91"> <div align="center">Unsubscribe </div></th>
</tr>
<?php
while ($objResult2 = mysql_fetch_array($objQuery2))
{
?>
<tr>
<td><div align="center"><?=$objResult2["Mail_ID"];?> </div></td>
<td><div align="center"><?=$objResult2["Status"];?> </div></td>
<td><div align="center"><?=$objResult2["Datum"];?> </div></td>
<td><div align="center"><?=$objResult1["Mail_ID"];?><input type="hidden" name="mailid[]" value="<?=$objResult1["Mail_ID"];?>"> </div></td>
<td><div align="center"><?=$objResult1["Titel"];?> </div></td>
<td><div align="center"><input type="radio" name="sub[<?=$objResult1["Subscriptions_ID"];?>]" value="10"> </div></td>
<td><div align="center"><input type="radio" name="sub[<?=$objResult1["Subscriptions_ID"];?>]" value="90"> </div></td>
</tr>
<?php
}
?>
</table>
如果您使用LEFT JOIN,则所有邮件行都将可见但如果您使用INNER JOIN,那么只有那些具有与其关联的订阅行的邮件行