Getting column values as array, Zend_Db, Mysql, php -
i have following database tables:
- table:
person
columns:id, first_name, age, city, state
- table:
study
columns:id, name, description, notes
- table:
person_studies
columns:person_id, study_id, notes
i need study names particular person involved in:
person.id, person.first_name, person.city, study.name
this query wrote data person.id = 14
:
select person.id, person.first_name, study.name person left join person_studies on person.id = person_studies.person_id left join study on person_studies.study_id = study.id person.id=14;
since there multiple studies person involved in, getting more 1 row in result set. need implement using zend_db api's.
the questions are:
- 1. there better way write query?
- 2. if want study.name values in separate array part of result set, possible in query such when run query in zend:
`$result = $this->fetchall($select);`$select sql statement $result should of following format:
[0] => array ( [person.id] => 14 [first_name] =>jamie [study_names] => array ( [0] => biotechnology; [1] => mathematics; [2] => aeronautics; ) )
any appreciated.
thanks
i not find better way write query, far second question goes, found following solution of using group_concat().
the final query looks now:
select person.id, person.first_name, person.city, person.state, group_concat(study.name separator '|') studynames person left join person_studies on person.id = person_studies.person_id left join study on person_studies.study_id = study.id person.id=14;
we can specify other clauses within group_concat eg distinct . please refer link http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat detailed description clause.
Comments
Post a Comment