我有两张桌子

(1) Table Names
      (1) id
      (2) name

(2) Table Numbers
    (1) id
    (2) userid
    (3) number
查询:
$GrabNumbers = mysql_query("SELECT name,number FROM Names
JOIN Numbers ON
Numbers.useid = name.id");

当我加入表格时,它会重复如下名称:john:+1 508 078 1540 john:+1 087 540 4508
我需要的是:
 john : +1 508 078 1540
      : +1 087 540 4508

最佳答案

是的,这就是MySQL所做的。如果您不希望它们在输出中重复出现,则需要创建一个PHP脚本以正确输出它们。

像这样:

$printed = array();
foreach( $rows as $row ) {
  if( isset ( $printed[ $row['name'] ] ) ) {
    // print only the number
  }
  else {
    // print both number and name

    // then, add the name to the list of names already printed
    $printed[$row['name']] = true;
  }
}


并确保按名称对行进行排序!否则,您可能会得到无法预测的(错误的)结果。

关于php - PHP&MySql Join重复该值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26730871/

10-11 05:22
查看更多