Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想要改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

5年前关闭。



Improve this question




如果仅用244代替前一个%s,我得到的总和将没有任何问题。但是在这种情况下使用动态值$ shot时,它似乎什么也没得到,并且我的查询也没有失败,因为其他结果也很好(例如,stories.id,stories.title)。
$query = sprintf("
SELECT
stories.id,
stories.title,
stories.timestamp,
stories.text,
users.name,
users.avatar,
users.id AS idus,
(SELECT sum(reviews.amount) FROM reviews WHERE reviews.storyid='%s') AS reviews

FROM stories INNER JOIN users ON stories.uid=users.id WHERE stories.id = '%s'",
    mysql_real_escape_string($shot),
    mysql_real_escape_string($shot));

射击来自这里:
$shot = $_GET['shot'];

最佳答案

我会写这样的东西。

SELECT
stories.id,
stories.title,
stories.timestamp,
stories.text,
users.name,
users.avatar,
users.id AS idus,
SUM(reviews.amount) as reviews
FROM stories
INNER JOIN users ON stories.uid=users.id
INNER JOIN reviews ON stories.id = reviews.storyid
WHERE stories.id = '%s'"

这与您的问题无关,而与优化有关。

10-04 13:34