我知道这个问题出现在很多网站上,但仍然存在。
这是在Code中调用的Query:
$exec = $dbh->prepare($query); //query as shown below
pr($exec); // output for me
$exec->execute();
我从pdo或任何事情中都没有错误。 如果我以1:1复制查询,我会得到正确的值。
错误的价值是'总和'和' sum_bank',所以计算也是错误的。
INSERT INTO `db`.`inserttable`
SELECT null as `id`, `state`,`company`.`rid`,
8 as `month`,
19 as `day`,
(
SELECT count(company2.cid)
FROM `company` as `company2`
LEFT JOIN `db`.`gid` ON `company2`.`cid` = `db`.`gid`.`cid`
WHERE `company2`.`state` = `company`.`state` AND gid.`gid` = `company`.`rid`
) as `sum`,
SUM(IF(`ctype` = 'company', 1, 0)) as `sum_company`,
(
SELECT count(company2.cid)
FROM `company` as `company2`
LEFT JOIN `db`.`gid` ON `company2`.`cid` = `db`.`gid`.`cid`
WHERE `company2`.`state` = `company`.`state` AND `gid`.`gid` = `company`.`rid` AND `ctype` = 'bank'
) as `sum_bank`,
(SUM(IF(`ctype` = 'company', 1, 0)) / (SELECT `sum`)) * 100 as `prc_company`,
(SUM(IF(`ctype` = 'bank', 1, 0)) / (SELECT `sum`)) * 100 as `prc_bank`,
SUM(`isBir`) as `sum_isBir`,
SUM(`isCgr`) as `sum_isCgr`,
SUM(`isBar`) as `sum_isBar`,
(SUM(`isBir`) / (SELECT `sum`)) * 100 as `prc_isBir`,
(SUM(`isCgr`) / (SELECT `sum`)) * 100 as `prc_isCgr`,
(SUM(`isBar`) / (SELECT `sum`)) * 100 as `prc_isBar`
FROM `db`.`company`
GROUP BY `db`.`company`.`state`, `db`.`company`.`rid`
用于错误检查:
$dbh = Database::getInstance();
try {
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$exec = $dbh->prepare($query);
pr($exec);
$exec->execute();
pr($dbh->errorInfo());
} catch(PDOException $e) {
return array('Exception' => array('code' => $e->getCode(), 'message' => $e->getMessage()));
}
答案是:
array(1) {
[0]=>
string(5) "00000"
}
如此认真......我没有错误
我可以自己解决:
错误是:来自company
company2
转为:FROM db
。company
为company2
在php我的管理员中我选择了正确的数据库,因为它在那里工作。
遗憾的是,它没有以任何形式犯错误,这会让我节省很多时间