1

我有两张桌子mysql。以下是我的脚本。

<?php
$conn=mysql_connect("localhost",'root','');
mysql_select_db('test');

$sql    = "SELECT data_student.name,ozekimessagein.msg 
            FROM ozekimessagein,data_student
            WHERE ozekimessagein.sender=data_student.sender 
            ORDER BY ozekimessagein.msg";

$res=mysql_query($sql);
$cn=mysql_num_rows($res);
for($x=0;$x<$cn;$x++)
{
    list($name,$msg)=mysql_fetch_row($res);
    echo "<li>$name,$msg";
}
mysql_close($conn);
?>

只要表中的发件人与表中的发件人匹配,这应该显示表中的名称data_student和消息。但它不起作用。ozekimessageindata_studentozekimessagein

4

2 回答 2

0

尝试这个

 <?php
  $conn=mysql_connect('localhost','root','');
    mysql_select_db('test');

 $sql    = "

   SELECT s.name AS names, o.msg AS msgs
  FROM ozekimessagein o
  INNER JOIN data_student s
   ON o.sender = s.sender
   ORDER BY s.name";

  $res=mysql_query($sql);

  while ($row = mysql_fetch_assoc($res)) {

 echo "<li>".$row['names']." : ".$row['msgs']."</li>";
     }
  mysql_close($conn);
  ?>
于 2012-11-27T09:03:43.010 回答
0

尝试这个 ::

SELECT 

data_student.name,
ozekimessagein.msg 

FROM ozekimessagein 
inner join data_student     on ozekimessagein.sender=data_student.sender 

ORDER BY ozekimessagein.msg
于 2012-11-27T08:31:45.450 回答