【问题标题】:Select and unlink file on 3 tables选择并取消链接 3 个表上的文件
【发布时间】:2018-02-06 07:15:24
【问题描述】:

请帮我检查我的查询。我进行了很多搜索,并且之前没有尝试选择 3 个表。

我认为我做对了,但我不知道为什么什么都没发生。

  public function delSection($delete_id)
  {
    $stmt = $this->conn->prepare("SELECT * FROM tbl_section
                                           JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
                                           JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
                                           WHERE tbl_section.sec_id=:del_id");
    $stmt->execute(array(":del_id"=>$delete_id));
    while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
    {
      unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
      unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
      rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
    }

    $stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
                                          JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
                                          JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
                                          WHERE tbl_section.sec_id=:del_id");

    $stmt2->bindparam(":del_id",$delete_id);
    $stmt2->execute();
    return true;
  }

我要做的是从 3 个表中选择 * 并使用 fk sec_id 获取它们的数据

这里是查询的手动运行

链接:

代码:

【问题讨论】:

  • 你在 WHERE 子句中有 "table_files.sec_id" .... 但我在 SELECT 或 JOIN 中看不到它,所以我不知道你想在那里做什么.另外,你得到什么错误?当您独立运行 select 子句时会发生什么?您使用显式连接而不是简单连接的任何原因?
  • 我会编辑我的帖子,请稍等一下
  • “sec_id”列在两个或多个表中,因此您可以在 where 子句中用作“tbl_section.sec_id”
  • 没有错误,但我什么也没做
  • @McC0d3 我看到你的tbl_content 没有数据

标签: php mysql sql database pdo


【解决方案1】:

使用 LEFT OUTER JOIN QUERY 完成

    $stmt = $this->conn->prepare("SELECT * FROM tbl_section
                                           LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
                                           LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
                                           WHERE tbl_section.sec_id=:unlink_id");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2021-11-19
    • 2015-09-25
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多