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

Posted

技术标签:

【中文标题】不是唯一的表/别名(codeigniter)【英文标题】:Not unique table/alias (codeigniter) 【发布时间】:2019-07-13 15:50:03 【问题描述】:

错误号:1066

不是唯一的表/别名:'posts'

SELECT * FROM (SELECT id, title, keywords, 'posts' AS type FROM posts, posts UNION SELECT id, title, keywords, 'android' AS 输入 FROM android, android UNION SELECT id, title, keywords, 'mac' AS type FROM mac, mac)t WHERE t.title like '%yj%' OR 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();

【问题讨论】:

FROM posts, posts ?你想做什么? 喜欢来自 3 个表的帖子,android,mac 【参考方案1】:

删除所有表的第二个名称并改用 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%'

【讨论】:

以上是关于不是唯一的表/别名(codeigniter)的主要内容,如果未能解决你的问题,请参考以下文章

不是唯一的表/别名

错误代码:1066。不是唯一的表/别名:'circle_call_prefixes'

不是唯一的表/别名:'movie_direction' [关闭]

发生数据库错误错误号:1066 不是唯一的表/别名:[重复]

错误代码:1066。不是唯一的表/别名:'ordertbl' - 无法执行内部连接 ​​MYSQL

为啥错误 #1066 - 不是唯一的表/别名:'cat_rapoarte'