mysql从3个表连接查询

时间:2014-09-22 18:18:30

标签: php mysql join

我想要来自三个表1 tbl_customerinfo,2 tbl_customerinfo,3- tbl_cust_ext_pref;

的连接查询

我想在此查询中添加到表3(tbl_cust_ext_pref)..

我使用了来自两个表的连接查询并获取所有信息和代码正在运行a-tbl_customerinfo领​​域名称(id,c_shortcode,c_type)

b - tbl_cust_extinfo

feild name(cx_id,cx_gender,cs_createdby)

我使用此代码

/ *代码在这里* /

$this->db->select ( this is a table

                  'tbl_customerinfo.c_id,
                  tbl_customerinfo.c_shortcode,
                   tbl_customerinfo.c_type,

/*this is b table tbl_cust_extinfo.cx_id, tbl_cust_extinfo.cx_gender, tbl_cust_extinfo.cs_createdby*/

                     ');

           $this->db->from('tbl_customerinfo');
       $this->db->join
       (
        'tbl_cust_extinfo', 
        'tbl_customerinfo.c_id = tbl_cust_extinfo.cx_id'

        );

        $query = $this->db->get();
//$info='tbl_customerinfo';
if($query->num_rows() > 0)
{

    foreach ($query->result() as $row)
    {
        //print_r($row);
        //exit();


        $info[] = array(
                         'c_id' => $row->c_id,
                         'c_shortcode' => $row->c_shortcode,
                         'c_type'=> $row->c_type


                         array(

                                 'cx_id'=> $row-> cx_id,
                                 'cx_gender'=> $row -> cx_gender);

1 个答案:

答案 0 :(得分:0)

在单个查询中编写一次sql查询:

$sql = 'select * from table1 join table2 on table1.c1 = table2.c2 '
        . 'join table3 on table1.c1 = table3.c3';
$res = mysql_query($sql);