简体   繁体   English

php:对于一个mysql表中的每个记录,显示第二个表中的所有记录

[英]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.

相关问题 从一个表中选择所有记录,并检查它们是否在第二个表中是公用的 - Select all records from one table and check them if they are common in 2nd table MYSQL Update单个记录第二个表MYSQL中多个记录的MIN&MAX值的表 - MYSQL Update single records a table with MIN & MAX values from multiple records in 2nd table MYSQL 列出第三张表的所有记录,但第二张表是 pivot 表 - MySQL Laravel - List all record with count of third table but 2nd table is pivot table - MySQL Laravel PHP,如何显示第二个表 - PHP, How to display the 2nd table 我想连接数据并显示一个表中的所有记录和另一个表中的一个记录 - I want to join data and display all records from one table and just one record from another table Codeigniter 显示表 2 中每条 id 与表 1 相似的记录的记录 - Codeigniter display records from table 2 for each record with id similar to table 1 使用第二个表从1个表进行MYSQL排序数据 - MYSQL ordering data from 1 table using the 2nd table 通过使用 php 查看表中的条件从第二个表中提取数据 - Extracting data from the 2nd table by looking at the condition in a table with php HTML 表:从一列(每行)和第二列添加值并将其显示在第三列上 - HTML Table : addition of value from one column(each row) and 2nd column and dispaly it on third column 如何在MySQL中选择两个表,但仅从第二个表中选择一列? - How can I select two tables in MySQL but only select one column from the 2nd table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM