如何从3个表中获取数据?功能

时间:2017-05-28 16:39:43

标签: php mysql function codeigniter module

我有错误如何修复? “where子句中的列'标题'含糊不清”:

    get_results_from($search_term='default')
    $targets=array('a_table','b_table','c_table');
    $this->db->select('*');
    $this->db->from($targets);
    $this->db->like('title',$search_term);
    $this->db->or_like('body', $search_term);
    $query = $this->db->get();
    return $query->result_array();

2 个答案:

答案 0 :(得分:0)

尝试

<?php
get_results_from($search_term='default');
$sql =  "SELECT * from a_table where `title` LIKE '%".$search_term."%' OR `body` LIKE '%".$search_term."%'
        UNION
        SELECT * from b_table where `title` LIKE '%".$search_term."%' OR `body` LIKE '%".$search_term."%' 
        UNION
        SELECT * from c_table where `title` LIKE '%".$search_term."%' OR `body` LIKE '%".$search_term."%' ";
$query = $this->db->query($sql);
return $query->result_array();
?>

答案 1 :(得分:0)

试试这段代码:

function get_results_from($search_term='default')
{
    $targets=array('a_table','b_table','c_table');
    $this->db->select('*');
    $this->db->from($targets);
    $this->db->group_start();
       $this->db->like('a_table.title',$search_term);
       $this->db->or_like('b_table.title',$search_term);
       $this->db->or_like('c_table.title',$search_term);
       $this->db->or_like('body', $search_term);
    $this->db->group_end();
    $query = $this->db->get();
    return $query->result_array();
}