当我在查询中使用Union All时,Codeigniter返回0结果

时间:2016-06-07 14:47:45

标签: php mysql codeigniter

$sql = 'SELECT bind_id, Title, category, image_url, keyword, style_Binder_id, end_user_id, NULL AS cphoto_id, NULL AS cphoto, NULL AS title, NULL AS company_id, DATE FROM styleBinds  
UNION ALL  
SELECT NULL , NULL , NULL , NULL , keyword, style_Binder_id, end_user_id, cphoto_id, cphoto, title, company_id, date_time FROM company_photo  
ORDER BY DATE ASC';
$sql =$this->db->query($query);
if($sql->num_rows > 0){
    $result=$sql->result();
    return $result;
}else{
    echo "0 records";
}

当我在phpmyadmin中运行此查询时,它返回150结果

但在codeignitor中返回0结果......

2 个答案:

答案 0 :(得分:0)

简单的错字问题。你已经颠倒了论点和作业。 代码应该是

$query=$this->db->query($sql);

if($query->num_rows() > 0){
  return $query->result();
}else{
  return NULL; //controller should decide what to do with no model results
}

答案 1 :(得分:0)

试试这个:假设你的表中有date_time,我建议使用isnull代替null

SELECT bind_id, Title, category, image_url, keyword, style_Binder_id, end_user_id, NULL AS cphoto_id, NULL AS cphoto, NULL AS title, NULL AS company_id,date_time FROM styleBinds 
UNION ALL  
SELECT NULL as bind_id , NULL as Title , NULL as category , NULL as image_url,keyword, style_Binder_id, end_user_id, cphoto_id, cphoto, title, company_id, date_time FROM company_photo  
ORDER BY date_time ASC;