[英]php: for each record in one mysql table, display all the records from a 2nd table
I have 2 tables, one with email addresses, and one with a number of rows per email address: 我有2个表,一个带有电子邮件地址,一个带有每个电子邮件地址的行数:
Table 1:
1@gmail.com
2@gmail.com
etc
Table 2:
1@gmail.com,value111,value112,value113
1@gmail.com,value121,value122,value123
2@gmail.com,value211,value212,value213
etc.
I want to send each of the email addresses their values, so I get the email: 我想发送每个电子邮件地址的值,所以我收到了电子邮件:
$query = "SELECT email FROM email_table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$email = $row['email'];
and then, I need to get the values for each email. 然后,我需要获取每封电子邮件的值。 I tried using foreach but apparently not in a correct way Can anybody help?
我尝试使用foreach,但显然使用方式不正确,有人可以帮忙吗?
Many thanks! 非常感谢!
$query = "SELECT * FROM email_table AS table1 LEFT JOIN table2 ON table2.email = table1.email";
--edit-- after clarification, you want to get the info from table 2 attached to the email address in table 1 and send an email to table 1... --edit--在澄清之后,您想要从表2中获取信息并附加到表1中的电子邮件地址,并将电子邮件发送到表1中。
$query = "SELECT table1.email, table2.* FROM email_table AS table1 LEFT JOIN table2 ON table2.email = table1.email";
Your result here is the email field from table 1 with all information from table 2 joined on the same email address. 您在这里得到的结果是表1中的电子邮件字段,表2中的所有信息都加入了相同的电子邮件地址。 You don't need both emails returned here so in your field list, put something like this instead of
table2.*
: 您不需要在此都返回两个电子邮件,因此在您的字段列表中,输入以下内容代替
table2.*
:
SELECT table1.email, table2.field1, table2.field2, table2.field3
Then you can use PHP's mail
to send to the email you get from table1. 然后,您可以使用PHP的
mail
发送到从table1获得的电子邮件。
You are wanting to do a JOIN
. 您想做一个
JOIN
。 So your query will look like this: 因此,您的查询将如下所示:
SELECT * FROM table2 JOIN table1 ON (table2.email = table1.email);
Coding Horror has a great explanation of all the join types between the tables. 编码恐怖对表之间的所有联接类型都有很好的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.