我想要结合这两个查询。我已经做了一些没有成功的证据,显然我错误地用正确的模式命令单词
$qryString="INSERT INTO votation (userID, voto, messageID) VALUES (:user, :voto, :mID)
ON DUPLICATE KEY UPDATE voto = :voto";
$qryString="SELECT AVG(voto) AS average_valutation FROM votation WHERE messageID=:mID";
Hove将这两个查询统一在一起?
答案 0 :(得分:0)
$qryString="INSERT INTO votation (userID, voto, messageID) VALUES (:user, :voto, :mID)
ON DUPLICATE KEY UPDATE voto = :voto ;";
^----note this to separate from second query.
$qryString .="SELECT AVG(voto) AS average_valutation FROM votation WHERE messageID=:mID";
^---note this point.
答案 1 :(得分:0)
取决于您使用的PHP mysql库, 试试这个:
$results = array();
$qryString="
INSERT INTO votation (userID, voto, messageID) VALUES (:user, :voto, :mID)
ON DUPLICATE KEY UPDATE voto = :voto;
SELECT AVG(voto) AS average_valutation FROM votation WHERE messageID=:mID;
";
$mysqli->multi_query($qryString)
do {
if ($res = $mysqli->store_result()) {
$results[] = $res->fetch_all(MYSQLI_ASSOC);
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
http://php.net/manual/de/mysqli.multi-query.php
(已弃用的)mysql_
函数中未实现多个查询。
我不确定PDO。