0

我有一个电子邮件功能,如下所示:

function send_email_with_attachment($to, $from, $email_att, $subject, $body)

我有 2 个表,此函数从中获取数据以发送电子邮件:

  1. 电子邮件
  2. 电子邮件附件

电子邮件具有以下列:

  1. 序列
  2. 发邮件给
  3. 电子邮件来自
  4. 主题
  5. 信息

并且 *email_attachments* 具有以下列:

  1. 序列
  2. email_seq(匹配 emails 表中的序列)
  3. 附件

所以我希望能够从电子邮件表中选择一行,然后选择 *email_attachments* 表中的所有相关行,并在一封电子邮件中发送所有附件

最好的方法是什么?

我通常会做这样的事情:

$sql="SELECT * from table1 where col = 'condition' ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs))
{
    $sql2="SELECT * from table2 where table1_col = '".$result["col"]."' ";
    $rs2=mysql_query($sql2,$conn);
    $result2=mysql_fetch_array($rs2);
}....
4

2 回答 2

1

Use the following outer join:

SELECT e.sequence, e.emailto, e.emailfrom, e.subject, e.message, a.attachment
FROM emails e
LEFT JOIN email_attachments a ON a.email_seq = e.sequence
ORDER by e.sequence

Then loop over the rows from this. When the sequence is the same as the previous row, push the attachment onto an array of attachments (unless it's NULL, which will occur if there are no attachments associated with the email).

于 2013-10-10T08:58:19.237 回答
1

编写一个连接查询,然后创建一个数组并使用 foreach 循环。您的联接查询应该是这样的

"select e.email,e.sequence,e.emailto,e.emailfrom,e.subject,e.message,ea.email_seq, ea.attachment from email e join email_attachments ea on ea.email_seq = e.sequence;
于 2013-10-10T08:55:47.543 回答