从两个表中选择的SQL返回重复值 - Codeigniter [英] SQL selecting from two tables returns duplicate values - Codeigniter

查看:179
本文介绍了从两个表中选择的SQL返回重复值 - Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从两个表中检索一些所有数据。没有条件。但我的代码返回重复的数据值。
每个表格包含4行



中心tbl:





输出:





My Controller code:

  $ this-> load-> model(admindata); 
$ data ['query'] = $ this-> admindata-> getcentrelist();

$ this-> load-> helper('url');
$ this-> load-> view('admin / header');
$ this-> load-> view('admin / training',$ data);
$ this-> load-> view('admin / footer');

我对Model的查询是:

  public function getcentrelist()
{
$ this-> load-> database
$ query = $ this-> db-> query('SELECT centre_name,course_name from tbl_training_courses,tbl_traning_centres');
return $ query-> result();
}

查看:( training.php)

 <?php foreach($ query as $ row):?> 
< tr>
< td><?php echo $ row-> centre_name; ?>< / td>
< / tr>
<?php endforeach; ?>


解决方案

这是正常的,为什么不简单请求两个查询,每个表一个,然后将结果合并到一个数组?

  public function getcentrelist ()
{
$ this-> load-> database();
$ query_courses = $ this-> db-> query('SELECT course_name as name from tbl_training_courses');
$ query_centers = $ this-> db-> query('SELECT centre_name as name from tbl_traning_centres');
$ courses = $ query_courses-> result();
$ centers = $ query_centers-> result();
return array_merge($ courses,$ centers);
}

请注意,我已将fields_name和中心名称字段重命名为统一名称:name,这是必要的具有一致的合并表。


I have to retrieve some all data from two tables. There is no condition. But my code returns duplicated values of data. each table contain 4 rows

Centres tbl:

Training Course tbl:

Output:

My Controller code:

    $this->load->model("admindata"); 
    $data ['query'] = $this->admindata->getcentrelist(); 

    $this->load->helper('url');
    $this->load->view('admin/header');
    $this->load->view('admin/training',$data);
    $this->load->view('admin/footer');

My query on Model is:

public function getcentrelist()
{
    $this->load->database();
    $query=  $this->db->query('SELECT centre_name,course_name from tbl_training_courses, tbl_traning_centres'); 
    return $query->result();
}

View: (training.php)

<?php foreach($query as $row): ?>
        <tr>
            <td><?php echo $row->centre_name; ?></td>             
        </tr>
       <?php endforeach; ?>

解决方案

This is normal because of the implicit join you made! Why don't you simply request two queries, one per table, and then merge the results in one array?

public function getcentrelist()
{
    $this->load->database();
    $query_courses=  $this->db->query('SELECT course_name as name from tbl_training_courses');  
    $query_centers=  $this->db->query('SELECT centre_name as name from tbl_traning_centres'); 
    $courses = $query_courses->result();
    $centers = $query_centers->result();
    return array_merge($courses,$centers);
}

Note please that I have renamed the fields course_name and center name to a unified name: "name", this is necessary to have a coherent merged table.

这篇关于从两个表中选择的SQL返回重复值 - Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆