PHP,MySQL从多个表中按关键字搜索

时间:2012-03-29 10:03:24

标签: php mysql codeigniter

我有这样的代码:

function search_keyword(){
        $keyword = trim($_POST['keyword']);
        $search_explode = explode(" ", $keyword);
        $x = 0;

        $sql = " ( SELECT name AS type FROM global_info WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

          $sql .= " ) UNION ALL "; 

          $sql .= " ( SELECT name AS type FROM person WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

          $sql .= " ) UNION ALL ";

          $sql .= "( SELECT name AS type FROM event WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

        $sql .= " ) ";

        $q = $this->db->query($sql);
         return $q = $q->num_rows() == 0 ? FALSE :  $q->result();
    }

该功能正在运行。我不知道的是如何知道哪个数据来自哪个表,然后在页面上显示结果?

2 个答案:

答案 0 :(得分:1)

也许,在每个SELECT的结果中添加一个字段。

function search_keyword(){
        $keyword = trim($_POST['keyword']);
        $search_explode = explode(" ", $keyword);
        $x = 0;

        $sql = " ( SELECT name AS type, \"table_global_info\" as mytable FROM global_info WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

          $sql .= " ) UNION ALL "; 

          $sql .= " ( SELECT name AS type, \"person\" as mytable FROM person WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

          $sql .= " ) UNION ALL ";

          $sql .= "( SELECT name AS type, \"event\" as mytable FROM event WHERE ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
               $sql .= " name LIKE '%$each%'";}                          
            else {

                $sql .= " name LIKE '%$each%' ";
            }
        }

        $sql .= " ) ";

        $q = $this->db->query($sql);
         return $q = $q->num_rows() == 0 ? FALSE :  $q->result();
    }

问候!

答案 1 :(得分:0)

在select语句中插入标识表的新列:     SELECT名称为AS类型,'tab_x'为from_table FROM global_info WHERE     ....      SELECT name AS type,'tab_x1'as from_ FROM FROM global_info WHERE

当您使用返回的值时,请检查“from_table”字段以查看结果来自的原始表