|
我正在尝试计算单个用户发布的总评论数。这是表的表结构comments:1 `) [0 I" \1 {& S- p
CREATE TABLE `PLD_COMMENT` (
3 H/ E. v8 N3 V3 ^+ P `ID` int(11) NOT NULL auto_increment, * E6 d5 g$ H8 b5 n( J
`ITEM_ID` varchar(11) NOT NULL,5 c7 a' K! G A' R& D7 F
`USER_ID` varchar(11) NOT NULL,2 `4 {7 D/ p- X1 F9 S+ R
`USER_NAME` varchar(255) NOT NULL, 6 {# S8 E" s$ ~$ P9 i% ~4 N$ P
`COMMENT` longtext,, W) R; p6 s8 Z5 }7 o6 a/ M
`COMMENT_TITLE` varchar(255) default NULL,, h0 L' S7 `7 B7 \2 X$ g$ \. ^
`COMMENT_RATING` tinyint(1) default '1', , K. }) a; N. ?/ }/ @
`TYPE` int(11) NOT NULL,
( Q2 h1 m" x, {5 P0 o2 s `DATE_ADDED` timestamp NOT NULL |0 v" \9 u% W- b L y2 Q8 l
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,4 }+ Z* F1 Z" Z2 i% A" P5 v; L! }
`IPADDRESS` varchar(15) default NULL,. L( u! X' @2 Z" v& n) Z" K w$ V
`STATUS` varchar(11) NOT NULL, 7 `" z% h! N$ c- Z0 X+ X5 N
PRIMARY KEY (`ID`) " C3 u+ I. u$ s T | ]& W
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
/ B/ y D# O7 T" U# b. o这里是表结构user表
4 M Q8 }$ w" }: O+ w; BCREATE TABLE `pld_user`($ ]0 m- c7 C7 Y a# e
`ID` int(11) NOT NULL auto_increment,
0 W B/ v: [2 B: D' p `LOGIN` varchar(100) NOT NULL,
' l2 w% g. f2 ]! r, L. u5 G `NAME` varchar(255) NOT NULL,. h3 a( j: f- U [8 F
`PASSWORD` varchar(46) NOT NULL,
1 C! d! a3 G, ]2 U4 H$ O `LEVEL` tinyint(4) NOT NULL default '0',1 j/ A9 X7 @' C& W
`RANK` tinyint(4) NOT NULL default '0',# o% o2 z3 Y" Q ~8 @& X6 s2 l7 s1 b
`ACTIVE` tinyint(4) NOT NULL default '0',
% O- A; }; ?$ s% ?8 p `LAST_LOGIN` timestamp NOT NULL
1 h j G; k4 X* i* ~- b default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,5 D5 I9 H9 d" v+ B! B7 r2 t! o& k
`REGISTRATION_DATE` timestamp NOT NULL default '0000-00-00 00:00:00',
% E; w9 U# m$ d& r8 u% z+ d% S* b" v `AUTH_IMG` varchar(255) default NULL,7 i: A0 ]! p: L8 I; B% a6 N9 m) C
`AUTH_IMGTN` varchar(255) default NULL,
8 s# r/ \" K6 \, ^$ O' q `SUBMIT_NOTIF` tinyint(4) NOT NULL default '1',
+ _6 R E( d5 S. R. g) J `PAYMENT_NOTIF` tinyint(4) NOT NULL default '1',
8 w+ P" j4 H( x. f/ I `ADDRESS` varchar(255) default NULL,
: S- V8 g1 |# [6 y @. Y; L- r0 d `EMAIL` varchar(255) NOT NULL,0 j$ S5 q$ \0 t J
`WEBSITE` varchar(255) default NULL,
. X+ i+ u+ M3 ?% Z `WEBSITE_NAME` varchar(255) default NULL,
! W$ e \9 t- D `INFO` varchar(255) default NULL, : f. K k, S: \) D7 \* v3 i' e( z
`ANONYMOUS` tinyint(4) NOT NULL default '0', # K, Z" l7 E# t ?6 g
`LANGUAGE` varchar(2) default NULL, : T7 I9 {* ^7 r( r5 d% J
`AVATAR` varchar(100) default NULL,
: c7 S( S! o8 e$ e `ICQ` varchar(15) default NULL, - C% W: K$ P1 |! p$ C1 Z5 `
`AIM` varchar(255) default NULL,
9 R- J. p6 R1 E- z `YIM` varchar(255) default NULL, " m4 j) [5 `8 p& x! E" m# V
`MSN` varchar(255) default NULL,
) m. K' x6 N0 A; Z) W3 K- A- x$ B `CONFIRM` varchar(10) default NULL,
7 ]; F4 c* F5 M) d+ g8 y7 K `NEW_PASSWORD` varchar(46) default NULL,
9 @5 W$ M8 g4 I7 U2 G0 t; P" { `EMAIL_CONFIRMED` int(11) NOT NULL default '1',
J/ K; \2 V! A/ ` `LNAME` varchar(255) default NULL,7 b4 r" d2 L* Y& b
`CITY` varchar(255) default NULL, 3 h* |2 g" }0 r2 B5 O- I, O4 e0 @
`STATE` varchar(255) default NULL,, d) ?/ N( Y O' N* S* a8 r9 n
`DOB` date default NULL,# O; Z" h2 U" I% V
`UTYPE` tinyint(1) NOT NULL default '0',4 @2 T: Z" u: ~. n4 Q
PRIMARY KEY (`ID`) % j9 I1 U3 }: w! c8 J v( c
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 & H% c% r0 Q+ I8 d" _* h; b4 h& Y
COMMENT='Stores all the users with informations'
% h. Q( {3 d3 I- U! g6 Z" v) a这是我的查询:6 m, b0 i4 G. Y: u* B$ H
SELECT count(c.USER_ID) as total_commments_user ,
0 f, K$ C) b! o; f" [8 q c.*, u.NAME, l.TITLE as LINK_TITLE, u.AUTH_IMG
0 b5 w3 _ S1 M* {$ X, c0 ~9 PFROM `PLD_COMMENT` c
4 ?3 _# ]4 n$ n% R8 o) S. g9 h3 Y# ileft outer join `PLD_USER` u ON (u.ID = c.USER_ID)
; I b/ U& }7 a% }) q {$ E& yleft outer join `PLD_LINK` l ON (l.ID = c.ITEM_ID AND l.STATUS='2')
& X, U! E2 z: Y8 @WHERE c.TYPE = '1' , F9 a6 a' y: o
AND c.STATUS = '2' 0 w+ E% a: e5 W! F8 m' Q f
group by c.ID ORDER BY c.ID DESC LIMIT 0 , 3
X, O; Q5 `" O! n当我运行此查询时,在的每一行中都有1 total_comments_user。
5 e3 i0 j- p( ]- g- v# P0 |5 U任何的想法?1 G" s3 r" l0 }1 `# V& F$ j" d
5 ?8 j/ S# Z! z解决方案: |
|