mysql 计数无法正常工作?

发布于 2025-01-01 01:08:23 字数 2810 浏览 0 评论 0原文

我正在尝试计算单个用户发布的评论总数。这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2025-01-08 01:08:23

您需要将 SELECT 子句中选择的所有列(c.USER_ID 除外)添加到 GROUP BY 子句中,如下所示:

group by c.ID, c.otherfields, l.title,..

<强>编辑:我认为以下内容可以正常工作:

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) 
group by c.ITEM_ID, c.USER_ID
ORDER BY c.USER_ID, l.ID 

示例:
如果您有以下示例数据:

  • PLD_LINK:

    ID 状态标题    
    1 1 标题1
    2 2 标题2
    
  • PLD_USER:

    ID 名称
    8 马哈茂德
    9 艾哈迈德
    
  • PLD_COMMENT:

    ID ITEM_ID USER_ID 状态
    4 1 8 1
    5 1 8 1
    6 1 8 1
    7 2 8 2
    8 2 8 2
    9 1 9 1
    10 1 9 1
    

情况 1:用户 Mahmoud 显示两次:

然后,前面的查询将为您提供每个用户的评论数对于每个项目也如此:

total_commments_user  ID  ITEM_ID   USER_ID   Name
         3            4      1         8     Mahmoud
         2            7      2         8     Mahmoud
         2            9      1         9      Ahmed

请注意,用户 Mahmoud 以不同的计数显示两次,因为他有不同的 Item_Id


情况 2:用户 Mahmoud 仅显示一次:

如果您想获取每个用户对所有项目的评论数,那么您需要仅按 USER_ID 您将得到:

total_commments_user  ID  ITEM_ID   USER_ID   Name  
         5            4      1         8     Mahmoud
         3            9      1         9      Ahmed

正如您所见,用户 Mahmoud 仅显示一次,因为我们输入了 Item_Id

然后您可以按状态或其他方式进行过滤。

You need to add all the columns you are selecting in the SELECT clause except the c.USER_ID to the GROUP BY clause, like this:

group by c.ID, c.otherfields, l.title,..

EDIT: I think the following will work properly:

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) 
group by c.ITEM_ID, c.USER_ID
ORDER BY c.USER_ID, l.ID 

Example:
If you have the following sample data:

  • PLD_LINK:

    ID   STATUS   TITLE    
    1      1      title1
    2      2      title2
    
  • PLD_USER:

    ID     NAME
    8    Mahmoud
    9     Ahmed
    
  • PLD_COMMENT:

    ID   ITEM_ID USER_ID   STATUS
    4      1        8        1
    5      1        8        1
    6      1        8        1
    7      2        8        2
    8      2        8        2
    9      1        9        1
    10     1        9        1
    

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:

total_commments_user  ID  ITEM_ID   USER_ID   Name
         3            4      1         8     Mahmoud
         2            7      2         8     Mahmoud
         2            9      1         9      Ahmed

Notice that the user Mahmoud is displayed twice with a different count, becouse he has different Item_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:

total_commments_user  ID  ITEM_ID   USER_ID   Name  
         5            4      1         8     Mahmoud
         3            9      1         9      Ahmed

As you can see now the user Mahmoud is displayed only one time, becouse we ingonred Item_Id.

You can then filter by status or what ever.

戏剧牡丹亭 2025-01-08 01:08:23

这是因为 group by 子句将按所有选择条件对计数进行分组。因此,由于 l.title 值不同,您只能通过 l.title 获得用户 ID 的计数。

基本上,您将其中一列作为您选择的计数。即

(select count(*) from tables where) as totalcount   

返回将给您该用户 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

(select count(*) from tables where) as totalcount   

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文