【发布时间】:2019-09-18 10:27:14
【问题描述】:
我有一个 Mysql (mariaDB) 查询,其中包含一些在 phpmyadmin 终端中运行良好的连接。但是,如果我尝试在 php (pdo) 中执行,数据库的工作量会很大,直到我在 nginx 上得到一个坏网关。
如果我从查询中删除连接,它也适用于我的 php 脚本。
所以问题一定出在连接上,但这里有什么问题?
public function exportCSVById($id){
$this->db->query('
SELECT DISTINCT
positions_list.id,
positions_list.order_id,
positions_list.position,
positions_list.comment,
positions_list.result,
positions_list.created,
positions_list.modified,
positions_list.releaseSuS1,
positions_list.releaseSuS2,
positions_list.releaseCuS1,
positions_list.releaseCuS2,
us1.lastname as SuS1,
us2.lastname as SuS2,
orderstates.keynr,
orderstates.content,
companies.id,
companies.company,
users.id,
users.lastname,
ordertype_positions.keynr,
ordertype_positions.p_company,
ordertype_positions.content as orderstate,
repkey.keynr,
repkey.p_company,
repkey.content as repkey,
rk2.keynr,
rk2.p_company,
rk2.content as repkey2
FROM positions_list
JOIN ordertype_positions
ON positions_list.optionindex=ordertype_positions.keynr
AND ordertype_positions.p_company = positions_list.comp_id
JOIN repkey
ON positions_list.keyindex=repkey.keynr
AND repkey.p_company = positions_list.comp_id
JOIN repkey as rk2
ON positions_list.keyindex2=rk2.keynr
AND rk2.p_company = positions_list.comp_id
JOIN companies
ON positions_list.comp_id=companies.id
JOIN users
ON positions_list.uid=users.id
JOIN users as us1
ON positions_list.releaseSuS1=us1.id
JOIN users as us2
ON positions_list.releaseSuS2=us2.id
JOIN orderstates
ON positions_list.repstate=orderstates.keynr
WHERE
positions_list.order_id =:id
ORDER BY
positions_list.created ASC
');
$this->db->bind(':id', $id);
$results = $this->db->resultSet();
return $results;
}
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
【问题讨论】:
-
将
JOIN更改为LEFT JOIN -
@Gulshan:为什么?发帖人知道数据库模式,你我都不知道。 JOIN 与 LEFT JOIN 不同。 Normal JOIN 表示 INNER JOIN。 LEFT JOIN 表示 LEFT OUTER JOIN。