1

我有以下表格

我的列表表

playerId listId type
 50       10     0
 51       10     0

球员

id  x  xx etc
50  x  xx etc
51  x  xx etc

我想在我提供 listId 和 type 的地方运行一个查询,它将获取与该 listId 和 type 相关的所有玩家的列表

try {
    $conn = $this->GetDBConnection();
    $type = 0; // 0 REQUEST BY PLAYERS
    $statement = $conn->prepare('SELECT p.* FROM myListTable c, players p WHERE (c.listId = :listId ) AND (c.type = :type) AND ( p.id = c.playerId) ');
    $statement->bindParam(':listId', $listId, PDO::PARAM_INT);
    $statement->bindParam(':type', $type, PDO::PARAM_INT);
    $statement->execute();
    if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC))) {
        return false;
    }
    $conn = null;
} catch(PDOException $e) {
    throw $e;
}

这只是让我误会。我该怎么做才能修复此查询?

4

1 回答 1

4

其中一个错误是您的准备查询中的拼写错误:

$statement = $conn->prepare('SELECT p.* 
    FROM myListTable c, players p 
    WHERE (c.listId = :c.listId ) 
        AND (c.type = :type) 
        AND ( p.id = c.playerId) ');

应该:c.listId只是。:listId

第二个是,type是MySQL中的保留字。您需要反引号才能在查询中对其进行转义。

另一个是,因为您只是从players表中选择值;aJOIN是首选。

查询应该是:

SELECT p.*
FROM players p
JOIN myListTable c
    ON (p.id = c.playerId)
WHERE (c.listId = :c.listId)
    AND (c.`type` = :type)
于 2013-03-18T08:30:09.807 回答