【问题标题】:mysql join query from 3 tables来自3个表的mysql连接查询
【发布时间】:2014-11-16 19:22:39
【问题描述】:

我想从三个表中加入查询 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

字段名称(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);

【问题讨论】:

  • 具体是什么问题?其余代码在哪里?它甚至有什么作用?
  • 我想再多一张表加入这个查询 这个表名是 tbl_cust_ext_pref 一个字段名 cid ,mid 和 xven
  • 至少提到表格列。为什么需要加入 3 个表?
  • 我想再多一张表加入这个查询 这个表名是 tbl_cust_ext_pref 一个字段名 cid ,mid 和 xven

标签: php mysql join


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多