简体   繁体   中英

Count the number of unique rows in SQL table using a single common column

I have the following table for an events website in an SQL database:

usereventsid    event_id    user_id    reg_date    rating    comments    attend

81              92          1          NULL        NULL      NULL        1
82              90          1          NULL        NULL      NULL        1
83              91          1          NULL        NULL      NULL        1
84              88          1          2017-03-21  NULL      comment1    NULL
85              88          4          NULL        NULL      NULL        1
86              92          4          NULL        NULL      NULL        1

This table shows the id numbers of different events. If a user's id number appears in the table and has attend set to 1, they are attending the event. If a user's id number appears in the table but attend is null, they are not attending and have only commented on the event.

I want to be able to count the number of users that are attending each unique event. So in this case there are two users attending event 92 and 88, there is one user attending events 90 and 91. This is the information that I need to get from the table but I'm struggling to figure out how to do it.

At the moment here is my sql:

$eventsSql = "SELECT eventid, name, date, time, location, description
              FROM Events
              ORDER BY eventid";

$result = mysqli_query($conn, $eventsSql)
        or die(mysqli_error($conn));

while($event = mysqli_fetch_assoc($result))
            {
                  $id = $event['eventid'];
                  $name = $event['name'];
                  $date = $event['date'];
                  $time = $event['time'];
                  $location = $event['location'];
                  $des = $event['description'];


              $sql = "SELECT event_id, attend
                      FROM User_Events
                      WHERE event_id = '$id' AND attend = '1'";

              $attendanceResult = mysqli_query($conn, $eventsSql)
                           or die(mysqli_error($conn));

              $num = mysqli_num_rows($attendanceResult);

               echo "<!--echos html into the webpage-->";

In my head the second sql statement works as follows (I know this isn't correct because it's not producing the right result); the statement selects all of the rows that have the event id specified using the $id variable produced above and that also have attend set to 1. The number of these rows is then counted and the value placed in $num. What is actually happening is the statement is selecting every row in the table and $num is being set to 6. I don't understand why this is happening as $id should only match with one row in the case of values 90 and 91 and two rows with values 88 and 92.

Can someone help me figure this out please, thank you.

seems you need then number of distinct user attending an event

SELECT eventid, count(distinct user_id) 
from Events
where attend =1
GROUP BY eventid 

You can do this with one SQL statement using the GROUP BY clause and a sub query

SELECT SUM(user_count) FROM 
   (SELECT COUNT(user_id) AS user_count 
    FROM User_Events
    WHERE attend=1
    GROUP BY event_id ) AS event_count 

Should give you a single result with the number of users attending all your events.

Lol, my original sql statement worked I was just using the wrong variable in $attendanceResult = mysqli_query($conn, $eventsSql)

Thanks anyway guys.

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