这可能会帮助你,
$res = mysql_query('SELECT * FROM employee em LEFT JOIN organization org ON em.id = org.empid');
$op = array();
while($row = mysql_fetch_assoc($res))
{
print_r($row);
if(!isset($op[$row[org]])){
$op[$row[org]] = array();
}
array_push( $op[$row[org]], $row['fname']." ".$row['lname']);
}
echo json_encode($op);
以及基于我本地数据库中的条目的相应结果,
Array
(
[id] => 1
[fname] => john
[lname] => doe
[sno] => 1
[empid] => 1
[org] => Google
)
Array
(
[id] => 2
[fname] => will
[lname] => smith
[sno] => 2
[empid] => 2
[org] => Microsoft
)
Array
(
[id] => 3
[fname] => abdul
[lname] => raseed
[sno] => 3
[empid] => 3
[org] => Google
)
{"Google":["john doe","abdul raseed"],"Microsoft":["will smith"]}
注意:DDL 如下,
/*DDL Information*/
-------------------
CREATE TABLE `employee` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`fname` VARCHAR(256) DEFAULT NULL,
`lname` VARCHAR(256) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
CREATE TABLE `organization` (
`sno` INT(11) NOT NULL AUTO_INCREMENT,
`empid` VARCHAR(10) NOT NULL,
`org` VARCHAR(256) DEFAULT NULL,
PRIMARY KEY (`sno`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
节点:尝试使用最新的 mysql 函数,而不是弃用。