JOIN查询选择和计数

时间:2014-10-23 13:27:08

标签: php mysqli

我需要加入3个查询,而且我不熟悉group by。

这些是查询:

1º SELECT * FROM user_uploads ORDER by up_time DESC

// take img_id column from the user_uploads
2º SELECT COUNT(*) rowCount FROM img_likes WHERE img_id = ? AND user_id = ?

// take img_id column from the user_uploads 
2º SELECT COUNT(*) rowCount FROM img_likes WHERE img_id = ?

这就是我所拥有的:

SELECT *,
    (SELECT COUNT(*) FROM img_likes WHERE img_id = user_uploads.img_id AND user_id = ?),
    (SELECT COUNT(*) FROM img_likes WHERE img_id = user_uploads.img_id)
FROM user_uploads
ORDER BY up_time DESC

然后我需要检查行数,我想我不能只使用mysqli_num_rows($ ...)

if ($aItems = $mysqli->prepare("    SELECT *,
                                        (SELECT COUNT(*) FROM img_likes WHERE img_id = user_uploads.img_id AND user_id = ?),
                                        (SELECT COUNT(*) FROM img_likes WHERE img_id = user_uploads.img_id)
                                    FROM user_uploads
                                    ORDER BY up_time DESC")) {
    $aItems->bind_param('i', $id);
    $aItems->execute(); // get photos
    //$aItems->store_result();
    //$aItems->bind_result();
    $aItems->fetch();


    foreach ($Items as $ItemInfo) {

        $liked = mysqli_num_rows($Items); // what can I do here?
        $total_likes = mysqli_num_rows($Items);

        $img_id = $aItemInfo['img_id'];

        if ($liked == 0) {
            $like = 'Like';
        }
        else if ($liked == 1) {
            $like = 'Unlike';
        }

        $photo_list .= '
        // lista de fotos
        // boton like
        <span class="total_likes" id="lik' . $status_id . '">' . $totallikes . '</span>
        <a id="' . $total_likes . '" class="likes">' . $like . '</a>
        ';
    }
}

如何检查已加入查询的行数?

提前致谢!

1 个答案:

答案 0 :(得分:0)

你可以将它用于准备好的陈述。

mysqli_stmt_store_result( $items);
$row_cnt = mysqli_stmt_num_rows( $items);

请参阅此question