我正在尝试制作一个SQL语句,该语句从test_opplegg
中获取帖子,并尝试通过该帖子检索表test_thumbs
中的投票数。在表test_thumbs
中,有一列表示后ID(test_thumbs.OId
)和test_thumbs.IntValue
。 test_thumbs.IntValue
可以是1或0。我试图检索test_thumbs.IntValue
的总和,但是当我运行此代码时,即使sql语句应打印15个帖子,它也会在第一篇文章之后停止。
$sql = $conn->prepare("SELECT test_opplegg.Title, test_opplegg.id as oid, test_opplegg.Desc, test_opplegg.ShortDesc,test_opplegg.Type, test_opplegg.Approved, test_opplegg.Language, test_opplegg.Grade, test_opplegg.UserId, test_opplegg.Link, test_users.id, test_users.user_Username, test_users.user_Name
,SUM(test_thumbs.IntValue) ExecCount
FROM `test_opplegg`
INNER JOIN test_users ON test_opplegg.UserId = test_users.id
INNER JOIN test_thumbs ON test_thumbs.OId = test_opplegg.Id;
;");
//$sql->bind_param('i', $id);
$sql->execute();
$result = $sql->get_result();/* Get the result */
$num_of_rows = $result->num_rows;/* Get the number of rows */
if ($num_of_rows > 0) {
while ($row = $result->fetch_assoc()) {
$row_title = $row['Title'] ;
$row_id = $row['oid'] ;
$row_desc = $row['Desc'] ;
$row_shortdesc = $row['ShortDesc'] ;
$row_grade = $row['Grade'] ;
$row_type = $row['Type'] ;
$row_approved = $row['Approved'] ;
$row_lang = strtolower($row['Language']);
$row_uid = $row['UserId'] ;
$row_link = $row['Link'] ;
$row_countit = "'" . $row['ExecCount'] . "'" ;
$row_username = $row['user_Username'] ;
$row_name = $row['user_Name'] ;
如果我删除
,SUM(test_thumbs.IntValue) ExecCount
一切正常。但是,只要我写,SUM(test_thumbs.IntValue) ExecCount
,就只打印一行。因此,有可能在每个
test_opplegg.id
上运行SUM()函数,并且对于每一行,我也想对具有相同post-id的test_thumbs中的所有值求和。 最佳答案
看起来你想念
group by test_opplegg.Id
在查询中
更新:
因此将JOIN更改为
LEFT JOIN test_thumbs ON test_thumbs.OId = test_opplegg.Id
因此,如果
test_thumbs
中没有相应的记录,则总和为0关于php - SQL语句停止在SUM()函数上运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43406749/