mysql 计数无法正常工作?
我正在尝试计算单个用户发布的评论总数。这是 comments
表的表结构:
CREATE TABLE `PLD_COMMENT` (
`ID` int(11) NOT NULL auto_increment,
`ITEM_ID` varchar(11) NOT NULL,
`USER_ID` varchar(11) NOT NULL,
`USER_NAME` varchar(255) NOT NULL,
`COMMENT` longtext,
`COMMENT_TITLE` varchar(255) default NULL,
`COMMENT_RATING` tinyint(1) default '1',
`TYPE` int(11) NOT NULL,
`DATE_ADDED` timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`IPADDRESS` varchar(15) default NULL,
`STATUS` varchar(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
这是 user
表的表结构
CREATE TABLE `pld_user`(
`ID` int(11) NOT NULL auto_increment,
`LOGIN` varchar(100) NOT NULL,
`NAME` varchar(255) NOT NULL,
`PASSWORD` varchar(46) NOT NULL,
`LEVEL` tinyint(4) NOT NULL default '0',
`RANK` tinyint(4) NOT NULL default '0',
`ACTIVE` tinyint(4) NOT NULL default '0',
`LAST_LOGIN` timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`REGISTRATION_DATE` timestamp NOT NULL default '0000-00-00 00:00:00',
`AUTH_IMG` varchar(255) default NULL,
`AUTH_IMGTN` varchar(255) default NULL,
`SUBMIT_NOTIF` tinyint(4) NOT NULL default '1',
`PAYMENT_NOTIF` tinyint(4) NOT NULL default '1',
`ADDRESS` varchar(255) default NULL,
`EMAIL` varchar(255) NOT NULL,
`WEBSITE` varchar(255) default NULL,
`WEBSITE_NAME` varchar(255) default NULL,
`INFO` varchar(255) default NULL,
`ANONYMOUS` tinyint(4) NOT NULL default '0',
`LANGUAGE` varchar(2) default NULL,
`AVATAR` varchar(100) default NULL,
`ICQ` varchar(15) default NULL,
`AIM` varchar(255) default NULL,
`YIM` varchar(255) default NULL,
`MSN` varchar(255) default NULL,
`CONFIRM` varchar(10) default NULL,
`NEW_PASSWORD` varchar(46) default NULL,
`EMAIL_CONFIRMED` int(11) NOT NULL default '1',
`LNAME` varchar(255) default NULL,
`CITY` varchar(255) default NULL,
`STATE` varchar(255) default NULL,
`DOB` date default NULL,
`UTYPE` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
COMMENT='Stores all the users with informations'
这是我的查询:
SELECT count(c.USER_ID) as total_commments_user ,
c.*, u.NAME, l.TITLE as LINK_TITLE, u.AUTH_IMG
FROM `PLD_COMMENT` c
left outer join `PLD_USER` u ON (u.ID = c.USER_ID)
left outer join `PLD_LINK` l ON (l.ID = c.ITEM_ID AND l.STATUS='2')
WHERE c.TYPE = '1'
AND c.STATUS = '2'
group by c.ID ORDER BY c.ID DESC LIMIT 0 , 3
当我运行此查询时,我在 下的每一行中得到 1代码>total_comments_user
。
有什么想法吗?
I am trying to count the total comments posted by single user. Here is the table structure of the comments
table:
CREATE TABLE `PLD_COMMENT` (
`ID` int(11) NOT NULL auto_increment,
`ITEM_ID` varchar(11) NOT NULL,
`USER_ID` varchar(11) NOT NULL,
`USER_NAME` varchar(255) NOT NULL,
`COMMENT` longtext,
`COMMENT_TITLE` varchar(255) default NULL,
`COMMENT_RATING` tinyint(1) default '1',
`TYPE` int(11) NOT NULL,
`DATE_ADDED` timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`IPADDRESS` varchar(15) default NULL,
`STATUS` varchar(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Here is the table structure for user
table
CREATE TABLE `pld_user`(
`ID` int(11) NOT NULL auto_increment,
`LOGIN` varchar(100) NOT NULL,
`NAME` varchar(255) NOT NULL,
`PASSWORD` varchar(46) NOT NULL,
`LEVEL` tinyint(4) NOT NULL default '0',
`RANK` tinyint(4) NOT NULL default '0',
`ACTIVE` tinyint(4) NOT NULL default '0',
`LAST_LOGIN` timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`REGISTRATION_DATE` timestamp NOT NULL default '0000-00-00 00:00:00',
`AUTH_IMG` varchar(255) default NULL,
`AUTH_IMGTN` varchar(255) default NULL,
`SUBMIT_NOTIF` tinyint(4) NOT NULL default '1',
`PAYMENT_NOTIF` tinyint(4) NOT NULL default '1',
`ADDRESS` varchar(255) default NULL,
`EMAIL` varchar(255) NOT NULL,
`WEBSITE` varchar(255) default NULL,
`WEBSITE_NAME` varchar(255) default NULL,
`INFO` varchar(255) default NULL,
`ANONYMOUS` tinyint(4) NOT NULL default '0',
`LANGUAGE` varchar(2) default NULL,
`AVATAR` varchar(100) default NULL,
`ICQ` varchar(15) default NULL,
`AIM` varchar(255) default NULL,
`YIM` varchar(255) default NULL,
`MSN` varchar(255) default NULL,
`CONFIRM` varchar(10) default NULL,
`NEW_PASSWORD` varchar(46) default NULL,
`EMAIL_CONFIRMED` int(11) NOT NULL default '1',
`LNAME` varchar(255) default NULL,
`CITY` varchar(255) default NULL,
`STATE` varchar(255) default NULL,
`DOB` date default NULL,
`UTYPE` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
COMMENT='Stores all the users with informations'
Here is my query:
SELECT count(c.USER_ID) as total_commments_user ,
c.*, u.NAME, l.TITLE as LINK_TITLE, u.AUTH_IMG
FROM `PLD_COMMENT` c
left outer join `PLD_USER` u ON (u.ID = c.USER_ID)
left outer join `PLD_LINK` l ON (l.ID = c.ITEM_ID AND l.STATUS='2')
WHERE c.TYPE = '1'
AND c.STATUS = '2'
group by c.ID ORDER BY c.ID DESC LIMIT 0 , 3
When I run this query I got 1 in each row under total_comments_user
.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
SELECT
子句中选择的所有列(c.USER_ID
除外)添加到GROUP BY
子句中,如下所示:<强>编辑:我认为以下内容可以正常工作:
示例:
如果您有以下示例数据:
PLD_LINK:
PLD_USER:
PLD_COMMENT:
情况 1:用户
Mahmoud
显示两次:然后,前面的查询将为您提供每个用户的评论数对于每个项目也如此:
请注意,用户
Mahmoud
以不同的计数显示两次,因为他有不同的Item_Id
。情况 2:用户
Mahmoud
仅显示一次:如果您想获取每个用户对所有项目的评论数,那么您需要仅按
USER_ID
您将得到:正如您所见,用户
Mahmoud
仅显示一次,因为我们输入了Item_Id
。然后您可以按状态或其他方式进行过滤。
You need to add all the columns you are selecting in the
SELECT
clause except thec.USER_ID
to theGROUP BY
clause, like this:EDIT: I think the following will work properly:
Example:
If you have the following sample data:
PLD_LINK:
PLD_USER:
PLD_COMMENT:
Case 1: the user
Mahmoud
is displayed twice:Then, the previous query will give you the count of the comments for each User and for each item too, like this:
Notice that the user
Mahmoud
is displayed twice with a different count, becouse he has differentItem_Id
.Case 2: the user
Mahmoud
is diplayed only one time:If you want to get the count of comments for each user for all items then you will need to group by only the
USER_ID
and you will got:As you can see now the user
Mahmoud
is displayed only one time, becouse we ingonredItem_Id
.You can then filter by status or what ever.
这是因为 group by 子句将按所有选择条件对计数进行分组。因此,由于 l.title 值不同,您只能通过 l.title 获得用户 ID 的计数。
基本上,您将其中一列作为您选择的计数。即
返回将给您该用户 ID 的每个实例的计数,并且它将显示该用户 ID 的每一行中的计数。您必须添加条件子句以使子选择仅查看与该用户 ID 相关的行。
That is because the group by clause will group the count by all of the select criteria. So due to different l.title values you will get only a count of user id by l.title.
Basically you make one of your columns your select count. i.e
The return is going to give you the count for every instance of that user id and it will show that count in every row for that userid. you will have to add the conditional clauses to make the subselect only look at the rows relevant to that userid.