简体   繁体   中英

Returning each unique user from a MySQL table and also the count of the number of rows for each user

I am using the following MySQL query to generate a table for users in a database. The query is designed to just return one row for each user, even though there are multiple rows for each user. This works fine, however I also need to calculate the number of unique entries for each user, to enter into the table where it states HERE . Do I need to use another query to return the count for all entries, and if so how do I integrate this with the code I already have?

$query="SELECT from_user, COUNT(*) AS num FROM tracks GROUP BY from_user ORDER BY COUNT(*) DESC";
$result=mysql_query($query) or die(mysql_error());

        while ($row = mysql_fetch_array($result)) {
        $user = $row['from_user'];

        echo "<tr>";
        echo "<td>".$user."</td>";
        echo "<td>uploads (**HERE**)</td>";
        echo "<td>favourites (count)</td>";
        echo "</tr>";
        }
?>
</table>

Because you've already created the custom field 'num', you can use that to get the count!

Add the following line after user = ...

$count = $row['num'];

Then you can

echo "<td>uploads ($count)</td>";

It miss your table stucture to know your field name, but, if i well understand your question you can use count + distinct in mysql. You can check this answer too.

SELECT DISTINCT(from_user) AS user, 
COUNT(from_user) AS num 
FROM tracks 
GROUP BY from_user 
ORDER BY num DESC";

For the second problem you can doing a second query, or do a join tracks .

I think, in your case it's easier to you to do se second query inside the loop to get all detail from 'user' result.

$query1="SELECT DISTINCT(from_user), COUNT(*) AS num 
FROM tracks 
GROUP BY from_user 
ORDER BY COUNT(*) DESC";

$query2="SELECT * FROM tracks";

$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());

$user_array = array();

while ($row = mysql_fetch_array($result1)) {

    $user = $row['from_user'];
    $num = $row['num'];

    $uploads_array = array();

    while ($sub_row = mysql_fetch_array($result2)) { 
      if( $sub_row['from_user'] == $user ) {
            //for example only due to the unknown structure of your table
            $uploads_array[] = array( 
                "file_name" => $sub_row['file_name'], 
                "file_url" => $sub_row['file_url'] 
            ); 
      }
    }

    $user_array[] = array( 
        "name" => $user, 
        "num_entry" => $num, 
        "actions" => $uploads_array
    );

}

// now the table with all data is stuctured and you can parse it

foreach($user_array as $result) {
    $upload_html_link_arr = array();
    $user = $result['name'];
    $num_entry = $result['num_entry'];
    $all_actions_from_user_array = $result['actions'];

    foreach($all_actions_from_user_array as $upload) { 
        $upload_html_link_arr[] = sprintf('<a href="%s">%s</a>', $upload["file_url"],$upload["file_name"]);
    }

    $upload_html_link = implode(', ',$upload_html_link_arr);

    $full_row = sprintf("<tr><td>%s</td><td>uploads : %s</td><td>favourites (%d)</td></tr>", $user, $upload_html_link, $num_entry);

    // now just echo the full row or store it to a table for the final echo.
    echo $full_row;
}

I hope this help, mike

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM