我有以下数据库表:
person
列: id, first_name, age, city, state
study
列: id, name, description, notes
person_studies
列: person_id, study_id, notes
我需要获得特定人员参与的所有研究名称:
person.id, person.first_name, person.city, study.name
这是我为获取person.id = 14
:
select person.id, person.first_name, study.name from person
left join person_studies on person.id = person_studies.person_id
left join study on person_studies.study_id = study.id
where person.id=14;
由于一个人可能参与多项研究,因此我在结果集中获得了多行。我需要使用Zend_Db api来实现它。
问题是:
`$result = $this->fetchAll($select);`其中$ select是sql语句 $ result应该是以下格式:
[0] => Array ( [person.id] => 14 [first_name] =>Jamie [study_names] => Array ( [0] => biotechnology; [1] => mathematics; [2] => aeronautics; ) )醇>
非常感谢任何帮助。
由于
答案 0 :(得分:1)
我找不到更好的方法来编写查询,但就第二个问题而言,我找到了以下使用 group_concat()的解决方案。
最终查询现在看起来像这样:
select person.id, person.first_name, person.city, person.state, group_concat(study.name separator '|') as studynames from person
left join person_studies on person.id = person_studies.person_id
left join study on person_studies.study_id = study.id
where person.id=14;
我们还可以在group_concat中指定其他子句,例如distinct。有关此条款的详细说明,请参阅链接http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat。
答案 1 :(得分:0)
查询看起来很好,但您似乎错过了其中一个列(假设您确实需要使用person.city):
select person.id, person.first_name, person.city, study.name from person
left join person_studies on person.id = person_studies.person_id
left join study on person_studies.study_id = study.id
where person.id=14;
而不是:
select person.id, person.first_name, study.name from person
left join person_studies on person.id = person_studies.person_id
left join study on person_studies.study_id = study.id
where person.id=14;
至于结果,我不知道有什么方法可以直接按照你想要的格式进行格式化。如果你不介意一些代码,你可以尝试获取你得到的结果并格式化它们。假设您正在使用PDO(通常与Zend一起使用),它将如下所示:
$statement = $db->query(<<query from above goes here>>);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
$personStudies = array();
foreach($result as $row)
{
$personId = $row['person.id'];
$studyName = $row['name'];
$personStudies[$personId]['first_name'] = $row['first_name'];
$personStudies[$personId]['study_names'][] = $studyName;
}
foreach($personStudies as $personId=>$person)
{
$formattedRow = array();
$formattedRow['person.id'] = $personId;
$formattedRow['first_name'] = $person['first_name'];
$formattedRow['study_names'] = $person['study_names'];
$formattedResults[] = $formattedRow;
}
而$formattedResults
应该看起来就像你指明的那样。否则,您可以分两部分进行查询:
$statement = $db->query('SELECT person.first_name FROM person WHERE person.id=14 LIMIT 1');
$person = $statement->fetch(PDO::FETCH_ASSOC);
$name = $person['first_name'];
$statement = $db->query('SELECT study.name FROM person_studies, study WHERE person_studies.study_id = study.id AND person_studies.person_id=14'); // Same as a left join
$studyNames = $statement->fetch(PDO::FETCH_COLUMN, 0);
$formattedRow = array();
$formattedRow['person.id'] = 14;
$formattedRow['first_name'] = $name;
$formattedRow['study_names'] = $studyNames;
$formattedResults[] = $formattedRow;
应该给出相同的结果。我没有测试过代码,但它应该接近你需要的代码。