我有两张 table
(1) Table Names (1) id (2) name (2) Table Numbers (1) id (2) userid (3) number
the query :
$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 & MySql Join 重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26730871/