[英]How can i get the total of a column in mysql?
Alright, I am kind of new to SQL and I have no idea on how I am going to do this. 好吧,我是SQL的新手,我不知道该怎么做。 I have tried to google but maybe I'm not sure if I am searching for the right question.
我曾尝试用Google搜索,但也许不确定我是否在搜索正确的问题。 So here it is.
就是这样 I have 2 tables in my database.
我的数据库中有2个表。 One is called
events
and another one is called eventtypes
. 一种称为
events
,另一种称为events
eventtypes
。
My eventtype
table looks like this.. 我的
eventtype
表如下所示。
-------------------
eventID | eventName|
1 | eveA |
2 | eveB |
3 | eveC |
___________________
and my events
table, looks like this 和我的
events
表,看起来像这样
--------------------------
eventCat |UserRegistered |
1 | John |
2 | Mac |
3 | Assz |
2 | Ez |
3 | Pz |
_________________________
(I hope you understand my table.....) (希望您能理解我的桌子...。)
events.eventCat=eventtype.eventID
Now, what I am trying to do is to, calculate the numbers of participants for event 1, 2 and 3 in events
table and later display the number of users who will be attending appropriately by using the eventtype
table using php. 现在,我想做的是,在
events
表中计算事件1、2和3的参与者数量,然后通过使用php使用eventtype
表来显示将适当参加的用户数量。
Can somebody help me out with this ? 有人可以帮我吗? Thanks a bunch !
谢谢一群!
create table eventtype
( eventID int not null,
eventName varchar(100) not null
);
insert eventtype(eventID,eventName) values
(1,'eveA'),
(2,'eveB'),
(3,'eveC');
create table events
( eventCat int not null,
UserRegistered varchar(100) not null
);
insert events(eventCat,UserRegistered) values
(1,'John'),
(2,'Mac'),
(3,'Assz'),
(2,'Ez'),
(3,'Pz');
Query: 查询:
select e.eventId,count(u.UserRegistered) as theCount
from eventtype e
join events u
on u.eventCat=e.eventId
group by e.eventId
order by e.eventId;
+---------+----------+
| eventId | theCount |
+---------+----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
+---------+----------+
The followng query should work.it will select the no of participants based on the event using group by clause 接下来的查询应该起作用。它将使用group by子句根据事件选择参与者的数量
SELECT count(UserRegistered),eventName
From events,eventype
WHERE events.eventCat=eventtype.eventID
GROUP BY eventCat
You can apply join and group by to acheive this - 您可以应用join和group by来达到此目的-
SELECT count(UserRegistered),eventName from events join eventtype on
events.eventCat=eventtype.eventID
GROUP BY eventCat
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.