不是唯一的表/别名(codeigniter)

时间:2019-07-13 15:50:03

标签: php mysql sql codeigniter

  

错误号:1066

     

不是唯一的表格/别名:“帖子”

     

SELECT * FROM(选择idtitlekeywords,“帖子” AS类型FROM   postsposts UNION SELECT idtitlekeywords,'android'AS   键入FROM androidandroid UNION SELECT idtitlekeywords,   'mac'AS类型FROM macmac} t t.title如'%yj%'或   t.keywords like'%yj%'

型号:

  $query = $this->input->GET('search', TRUE);
  $this->db->select("id, title, keywords, 'posts' AS type");
  $this->db->from("posts");

  $query1 = $this->db->get_compiled_select('posts');

  $this->db->select("id, title, keywords, 'android' AS type");
  $this->db->from("android");

  $query2 = $this->db->get_compiled_select('android');

  $this->db->select("id, title, keywords, 'mac' AS type");
  $this->db->from("mac");

  $query3 = $this->db->get_compiled_select('mac');


  $data = $this->db->query('SELECT * FROM (' . $query1 . ' UNION ' . $query2 . ' UNION ' . $query3 . ')' . "t WHERE t.title like '%$query%' OR t.keywords LIKE '%$query%'");
         return $data->result();

1 个答案:

答案 0 :(得分:2)

删除表的所有第二个名称,并改用UNION ALL:

SELECT * FROM (
  SELECT id, title, keywords, 'posts' AS type FROM  posts 
  UNION ALL 
  SELECT id, title, keywords, 'android' AS type FROM android 
  UNION ALL 
  SELECT id, title, keywords, 'mac' AS type FROM mac
)t WHERE t.title like '%yj%' OR t.keywords LIKE '%yj%'