MySQL 查询错误 12

MySQL query ERROR 12

public function getAllSharedCases($uid) {
        $sql = "SELECT * FROM cases 
                INNER JOIN share_pathologist AS sp ON cases.case_id = sp.case_id 
                INNER JOIN share_group AS sg ON cases.case_id = sg.case_id 
                WHERE sp.pathologist_id = ? OR sg.group_id = (SELECT group_id FROM user_group WHERE state = ? AND user_id = ?)";
        $query = $this->db->prepare($sql);
        $query->execute(array($uid, "joined", $uid));
        if($query->rowCount()) {
            $cases = $query->fetchAll();
            for ($i = 0; $i < count($cases); $i++) {
                $shared = $this->getSharedWithGroups($cases[$i]["case_id"]);
                $cases[$i]["shared_group"] = $shared;
                $shared = $this->getSharedWithPathologists($cases[$i]["case_id"]);
                $cases[$i]["shared_pathologist"] = $shared;
            }
            return $cases;
        }
        return false;
    }

share_pathologist:

id
case_id
pathologist_id

share_group:

id
case_id
group_id

user_group:

id
user_id
group_id
state

此查询 returns 始终为假。

在子查询之前使用 IN 运算符而不是 EQUALS TO('=')

试试这个:

public function getAllSharedCases($uid) {
        $sql = "SELECT * FROM cases 
                INNER JOIN share_pathologist AS sp ON cases.case_id = sp.case_id 
                INNER JOIN share_group AS sg ON cases.case_id = sg.case_id 
                WHERE sp.pathologist_id = ? OR sg.group_id IN (SELECT group_id FROM user_group WHERE state = ? AND user_id = ?)";
        $query = $this->db->prepare($sql);
        $query->execute(array($uid, "joined", $uid));
        if($query->rowCount()) {
            $cases = $query->fetchAll();
            for ($i = 0; $i < count($cases); $i++) {
                $shared = $this->getSharedWithGroups($cases[$i]["case_id"]);
                $cases[$i]["shared_group"] = $shared;
                $shared = $this->getSharedWithPathologists($cases[$i]["case_id"]);
                $cases[$i]["shared_pathologist"] = $shared;
            }
            return $cases;
        }
        return false;
    }