2

我想访问 mysql 中的两个表,并想使用两个表中的字段。我使用内部连接来匹配两个表,但想知道如何获取两个表中的字段,因为两个表具有相同的字段名称和不同的值。这是我的查询...

$query1 = mysql_query("SELECT facilitator.FacID, facilitator.email, assigned.email FROM facilitator INNER JOIN assigned ON facilitator.FacID = assigned.FacID WHERE assigned.email = '$stuemail' AND facilitator.active = '1'") or die($query."<br/><br/>".mysql_error());
            while($line1=mysql_fetch_array($query1,MYSQL_ASSOC)){
                $faci= $line1['FacID'];
                $facemail = $line1['email'];
                $stumail2 = $line1['email'];
.............code goes on.............. 

我想获取两个电子邮件字段值,但想知道该怎么做。我确定我为 $stumail2 写的内容是错误的。请帮我。

4

3 回答 3

3

你有几个选择。

第一个是在 MySQL 中分配一个字段别名:

SELECT
    facilitator.FacID,
    facilitator.email AS facilitatorEmail,
    assigned.email AS assignedEmail
    ....

第二个,因为您正在使用mysql_fetch_array(),是通过索引访问字段。该facilitator.email字段具有 index1并且assigned.email具有 index 2,因此您可以使用:

$facemail = $line1[1];
$stumail2 = $line1[2];

或者,默认情况下,MySQL应该1为每个重复的列名添加一个递增的数字(从 开始)。因此,您应该能够使用email1来访问该assigned.email列:

$facemail = $line1['email'];
$stumail2 = $line1['email1'];
于 2012-10-09T18:35:50.490 回答
0

您可以在列上使用别名来消除歧义,例如:

SELECT facilitator.FacID, 
    facilitator.email as FacilitatorEmail, 
    assigned.email as AssignedEmail
FROM ...
于 2012-10-09T18:35:25.107 回答
0

您只需要查询中的“as”关键字:

$query1 = mysql_query("SELECT facilitator.FacID, facilitator.email as fac_email, assigned.email as assign_email FROM facilitator INNER JOIN assigned ON facilitator.FacID = assigned.FacID WHERE assigned.email = '$stuemail' AND facilitator.active = '1'") or die($query."<br/><br/>".mysql_error());
        while($line1=mysql_fetch_array($query1,MYSQL_ASSOC)){
            $faci= $line1['FacID'];
            $facemail = $line1['fac_email'];
            $stumail2 = $line1['assign_email'];
于 2012-10-09T18:38:28.143 回答