[英]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: 我在SQL数据库中有一个事件网站的下表:
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. 下表显示了不同事件的ID号。 If a user's id number appears in the table and has attend set to 1, they are attending the event.
如果用户的ID号出现在表中,并且出席人数设置为1,则他们正在参加活动。 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.
如果用户的ID号出现在表中,但出席人数为空,则表示他们没有参加,仅对活动发表了评论。
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.
因此,在这种情况下,有两个用户参加事件92和88,有一个用户参加事件90和91。这是我需要从表中获取的信息,但我一直在努力寻找方法。
At the moment here is my sql: 此刻是我的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); 在我的脑海中,第二条sql语句的工作方式如下(我知道这是不正确的,因为它无法产生正确的结果); 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.
该语句选择所有具有使用上面生成的$ id变量指定的事件ID的与会者行,并将与会者设置为1的所有行。然后对这些行的数量进行计数,并将值放入$ 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.
实际发生的情况是该语句正在选择表中的每一行,并且$ num被设置为6。我不明白为什么会这样,因为$ id仅在值90和91时与一行匹配,并且两行分别为88和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 您可以使用
GROUP BY
子句和一个子查询使用一条SQL语句来完成此操作
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)
大声笑,我原来的SQL语句的工作,我只是在
$attendanceResult = mysqli_query($conn, $eventsSql)
使用了错误的变量
Thanks anyway guys. 谢谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.