两个表的内连接查询没有给出确切的结果

时间:2014-05-28 10:35:08

标签: php codeigniter

我有两个表格,例如vw_ad_vehicle_detailsvw_ad_mobile_phones

$query = $this->db->select('AV.Title,AV.Description,AV.Price,AV.Created_Date,AMP.Title,AMP.Description,AMP.Price,AMP.Created_Date')
              ->distinct()
             ->from('vw_ad_vehicle AV')
             ->join('vw_ad_mobile_phones AMP','AV.Created_By = AMP.Created_By','inner')
             ->where('AV.Created_By and', $_SESSION['jobseeker_id'])
              ->where('AMP.Created_By',$_SESSION['jobseeker_id'])
             ->get();
            $applied_count=$query->num_rows();
            $applied_data=$query->result_array();
            var_dump($applied_data);

它不提供来自移动表的数据,它只给我车辆表中的数据 var_dump($applied_data)告诉我

array (size=22)
  0 => 
    array (size=4)
      'Title' => string 'New nokia lumia 630' (length=19)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '9876.00' (length=7)
      'Created_Date' => string '2014-05-19 15:42:07' (length=19)
  1 => 
    array (size=4)
      'Title' => string 'New nokia lumia 630' (length=19)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '9876.00' (length=7)
      'Created_Date' => string '2014-05-19 15:42:07' (length=19)
  2 => 
    array (size=4)
      'Title' => string 'Nokia X' (length=7)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '7623.00' (length=7)
      'Created_Date' => string '2014-05-19 16:32:27' (length=19)
  3 => 
    array (size=4)
      'Title' => string 'Nokia X' (length=7)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '7623.00' (length=7)
      'Created_Date' => string '2014-05-19 16:32:27' (length=19)
  4 => 
    array (size=4)
      'Title' => string 'Nokia 1100' (length=10)
      'Description' => string 'Nokia 1100 who is having blue body' (length=34)
      'Price' => string '1250.00' (length=7)
      'Created_Date' => string '2014-05-19 16:32:54' (length=19)
  5 => 
    array (size=4)
      'Title' => string 'Nokia 1100' (length=10)
      'Description' => string 'Nokia 1100 who is having blue body' (length=34)
      'Price' => string '1250.00' (length=7)
      'Created_Date' => string '2014-05-19 16:32:54' (length=19)
  6 => 
    array (size=4)
      'Title' => string 'Nokia X2-01' (length=11)
      'Description' => string 'Nokia X2-01 with headphone n charger' (length=36)
      'Price' => string '3999.00' (length=7)
      'Created_Date' => string '2014-05-19 16:33:05' (length=19)
  7 => 
    array (size=4)
      'Title' => string 'Nokia X2-01' (length=11)
      'Description' => string 'Nokia X2-01 with headphone n charger' (length=36)
      'Price' => string '3999.00' (length=7)
      'Created_Date' => string '2014-05-19 16:33:05' (length=19)
  8 => 
    array (size=4)
      'Title' => string 'Nokia 6600' (length=10)
      'Description' => string 'Nokia 6600 with gud quality' (length=27)
      'Price' => string '8777.00' (length=7)
      'Created_Date' => string '2014-05-19 16:33:19' (length=19)
  9 => 
    array (size=4)
      'Title' => string 'Nokia 6600' (length=10)
      'Description' => string 'Nokia 6600 with gud quality' (length=27)
      'Price' => string '8777.00' (length=7)
      'Created_Date' => string '2014-05-19 16:33:19' (length=19)
  10 => 
    array (size=4)
      'Title' => string 'Nokia 3300' (length=10)
      'Description' => string 'Nokia black 3300 with battery n headset' (length=39)
      'Price' => string '4532.00' (length=7)
      'Created_Date' => string '2014-05-19 16:34:01' (length=19)
  11 => 
    array (size=4)
      'Title' => string 'Nokia 3300' (length=10)
      'Description' => string 'Nokia black 3300 with battery n headset' (length=39)
      'Price' => string '4532.00' (length=7)
      'Created_Date' => string '2014-05-19 16:34:01' (length=19)
  12 => 
    array (size=4)
      'Title' => string 'New nokia lumia' (length=15)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '10234.00' (length=8)
      'Created_Date' => string '2014-05-19 16:34:13' (length=19)
  13 => 
    array (size=4)
      'Title' => string 'New nokia lumia' (length=15)
      'Description' => string 'new mobile is on sale' (length=21)
      'Price' => string '10234.00' (length=8)
      'Created_Date' => string '2014-05-19 16:34:13' (length=19)
  14 => 
    array (size=4)
      'Title' => string 'New nokia lumia' (length=15)
      'Description' => string 'sagj' (length=4)
      'Price' => string '12763.00' (length=8)
      'Created_Date' => string '2014-05-19 18:13:45' (length=19)
  15 => 
    array (size=4)
      'Title' => string 'New nokia lumia' (length=15)
      'Description' => string 'sagj' (length=4)
      'Price' => string '12763.00' (length=8)
      'Created_Date' => string '2014-05-19 18:13:45' (length=19)
  16 => 
    array (size=4)
      'Title' => string 'New Nokia XL' (length=12)
      'Description' => string 'sdfjk' (length=5)
      'Price' => string '7867.00' (length=7)
      'Created_Date' => string '2014-05-22 12:47:17' (length=19)
  17 => 
    array (size=4)
      'Title' => string 'New Nokia XL' (length=12)
      'Description' => string 'sdfjk' (length=5)
      'Price' => string '7867.00' (length=7)
      'Created_Date' => string '2014-05-22 12:47:17' (length=19)
  18 => 
    array (size=4)
      'Title' => string 'New Nokia XL' (length=12)
      'Description' => string 'sdfjk' (length=5)
      'Price' => string '7867.00' (length=7)
      'Created_Date' => string '2014-05-22 12:48:18' (length=19)
  19 => 
    array (size=4)
      'Title' => string 'New Nokia XL' (length=12)
      'Description' => string 'sdfjk' (length=5)
      'Price' => string '7867.00' (length=7)
      'Created_Date' => string '2014-05-22 12:48:18' (length=19)
  20 => 
    array (size=4)
      'Title' => string 'Brand New Nokia Lumia 630' (length=25)
      'Description' => string 'vdsv' (length=4)
      'Price' => string '12438.00' (length=8)
      'Created_Date' => string '2014-05-23 19:41:51' (length=19)
  21 => 
    array (size=4)
      'Title' => string 'Brand New Nokia Lumia 630' (length=25)
      'Description' => string 'vdsv' (length=4)
      'Price' => string '12438.00' (length=8)
      'Created_Date' => string '2014-05-23 19:41:51' (length=19)

请帮助我,并提前致谢。

1 个答案:

答案 0 :(得分:0)

您的select()函数错误,select的第二个参数用于转义,第一个参数包含select语句中的列列表

$this->db->select('AV.Title,AV.Description,AV.Price,AV.Created_Date,','AMP.Title,AMP.Description,AMP.Price,AMP.Created_Date')
                                                                     ^^^^ here

试试这个

$this->db->select('AV.Title,AV.Description,AV.Price,AV.Created_Date,AMP.Title,AMP.Description,AMP.Price,AMP.Created_Date')

修改

您在两个表中都有类似的列名,您还需要为它们提供唯一的别名,如下所示

$this->db->select('
AV.Title,
AV.Description,
AV.Price,
AV.Created_Date,
AMP.Title AS AMPTitle,
AMP.Description AS AMPDescription,
AMP.Price AS  AMPPrice,
AMP.Created_Date AS AMPCreated_Date'
)
相关问题