0

我有一个包含 3 个表(成员、船、船类型)的数据库,并且在下面的方法中,其中两个受到影响。我想要列出在会员上注册的所有船只,但代码有问题(至少是 SQL,但我怀疑这还不是全部)。

提前致谢。

船处理程序.php:

public function GetMembersBoats($memberId) {

    $query = "SELECT b.boatId, b.length, bt.type
                  FROM boat AS b
                      INNER JOIN member AS m
                          ON b.memberId = m.memberId
                              WHERE m.memberId = ?
                      INNER JOIN boatType AS bt
                          ON b.boatTypeId = bt.boatTypeId
                      GROUP BY b.boatId";

    $stmt = $this->m_db->Prepare($query);
    $stmt->bind_param('i', $memberId);

    $boats = $this->m_db->GetBoats($stmt);
}

数据库.php:

public function GetBoats($stmt) {
    $boats = array(
            0 => array(),
            1 => array(),
            2 => array()
        );

    $stmt->execute();

    $stmt->bind_result($boatId, $length, $type);

    while ($stmt->fetch()) {
        array_push($boats[0], $boatId);
        array_push($boats[1], $length);
        array_push($boats[2], $type);
    }

    $stmt->Close();

    return $boats;
}

受影响的表:

  • 船: boatId,boatTypeId,长度,memberId
  • 船型:船型ID,类型
4

2 回答 2

2

WHERE子句必须列在表引用之后。您必须在 and 之后将 移到表WHERE m.memberId = ?引用的末尾,如下所示:FROMJOIN

SELECT b.boatId, b.length, bt.type
FROM boat AS b
      INNER JOIN member AS m
          ON b.memberId = m.memberId

      INNER JOIN boatType AS bt
          ON b.boatTypeId = bt.boatTypeId
WHERE m.memberId = ?
GROUP BY b.boatId
于 2012-10-18T14:45:34.710 回答
1

在 on 子句中,只需将 'where' 替换为 'and':

SELECT b.boatId, b.length, bt.type
              FROM boat AS b
                  INNER JOIN member AS m
                      ON b.memberId = m.memberId
                          AND m.memberId = ?
                  INNER JOIN boatType AS bt
                      ON b.boatTypeId = bt.boatTypeId
                  GROUP BY b.boatId
于 2012-10-18T14:48:07.970 回答