【问题标题】:Not unique table/alias (codeigniter)不是唯一的表/别名(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

标签: php mysql sql codeigniter


【解决方案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%'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多