Php sql查询的问题
我有两个表Php sql查询的问题,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有两个表布道(布道id、布道id、布道名称等)和布道者(布道id、名、姓等)。现在我把它显示为第一个传教士的名字,然后是那个传教士的所有布道,等等,这是正常工作的。但是想展示有布道的传教士 使用了以下代码,但我只得到了第一个传教士 function viewAllpreachers($offset=0, $limit=null) { $preacher_ids = array(); $this->db->distinct('preacher_id')
布道(布道id、布道id、布道名称等)
和布道者(布道id、名、姓等)
。现在我把它显示为第一个传教士的名字,然后是那个传教士的所有布道,等等,这是正常工作的。但是想展示有布道的传教士
使用了以下代码,但我只得到了第一个传教士
function viewAllpreachers($offset=0, $limit=null) {
$preacher_ids = array();
$this->db->distinct('preacher_id');
$this->db->select('preacher_id');
$this->db->from('sermons');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $key => $res1) {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
} }
return false;
}
如果要显示带有布道的布道者,则应向查询中添加联接,这样,如果布道者没有布道,则不会出现在结果行集中 i、 e:
function viewAllpreachers($offset=0, $limit=null) {
$preacher_ids = array();
$this->db->distinct('preacher_id');
$this->db->select('preacher_id');
$this->db->from('sermons');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $key => $res1) {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->join('sermons', 'preacher.preacher_id = sermons.preacher_id');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
} }
return false;
}
注意:使用两个查询而不是一个查询是否有具体原因,您应该能够在一个SQL查询中检索布道者列表
function viewAllpreachers($offset=0, $limit=null) {
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->join('sermons', 'preacher.preacher_id = sermons.preacher_id');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
return false;
}
我将在修复代码的地方添加注释
function viewAllpreachers($offset=0, $limit=null)
{
$preacher_ids = array();
$this->db->distinct('preacher_id');
$this->db->select('preacher_id');
$this->db->from('sermons');
$query = $this->db->get();
$preachers = array();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $key => $res1)
{
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->where('preacher_id', $res1->preacher_id);
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row1)
{
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
$preachers[] = $preacher;
// I moved this code, so it will only add preachers who have sermons.
}
}
//return $preacher;
// If you return here, it'll always return the first preacher.
}
}
return $preachers;
// Here we return all preachers, that are added
}
return false;
}
难道这不是一个简单的内心连接的例子吗?我想展示所有有布道的传教士。如果传道者没有semons,则不会在代码中显示其详细信息,则您有
return$布道者
将在第一个$BREASTER
循环后返回并离开函数。将所有传教士保存在一个单独的数组中,然后返回传教士数组。
use this code
$preacher = array();
$this->db->select('*');
$this->db->from('preacher');
$this->db->join('preacher','sermons.preacher_id=preacher.preacher_id');
$this->db->order_by('first_name');
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
return false;
}