Re: query - select from one, update another

2005-08-08 Thread Michael Stassen
Jasper Bryant-Greene wrote: Michael Stassen wrote: Not exactly. They aren't the same. COUNT(id) counts distinct values of id, while COUNT(*) simply counts rows. [snip] Actually, COUNT(id) counts non-NULL values of id. COUNT(DISTINCT id) would count distinct values. http://dev.mysql.com

Re: query - select from one, update another

2005-08-08 Thread Jasper Bryant-Greene
Michael Stassen wrote: Not exactly. They aren't the same. COUNT(id) counts distinct values of id, while COUNT(*) simply counts rows. [snip] Actually, COUNT(id) counts non-NULL values of id. COUNT(DISTINCT id) would count distinct values. http://dev.mysql.com/doc/mysql/en/group-by-functi

Re: query - select from one, update another

2005-08-08 Thread Michael Stassen
Sebastian wrote: Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Hi. i came up with a similar query last night, but i didnt use group by. I have question, is it better to use COUNT(*

Re: query - select from one, update another

2005-08-08 Thread SGreen
Nuno Pereira <[EMAIL PROTECTED]> wrote on 08/08/2005 01:49:44 PM: > Sebastian wrote: > > Nuno Pereira wrote: > > > >> [EMAIL PROTECTED] wrote: > >> > >>> Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 > >>> 10:53:55 AM: > >>> > >>> > Gleb Paharenko wrote: > > > > Hello. > >

Re: query - select from one, update another

2005-08-08 Thread Nuno Pereira
Sebastian wrote: Nuno Pereira wrote: [EMAIL PROTECTED] wrote: Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 10:53:55 AM: Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid );

Re: query - select from one, update another

2005-08-08 Thread SGreen
Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 01:19:32 PM: > Nuno Pereira wrote: > > > [EMAIL PROTECTED] wrote: > > > >> Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 > >> 10:53:55 AM: > >> > >> > >>> Gleb Paharenko wrote: > >>> > >>> > Hello. > > > > What about

Re: query - select from one, update another

2005-08-08 Thread Sebastian
Nuno Pereira wrote: [EMAIL PROTECTED] wrote: Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 10:53:55 AM: Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Hi. i came up

Re: query - select from one, update another

2005-08-08 Thread Nuno Pereira
[EMAIL PROTECTED] wrote: Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 10:53:55 AM: Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Hi. i came up with a similar q

Re: query - select from one, update another

2005-08-08 Thread Nuno Pereira
[EMAIL PROTECTED] wrote: Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 10:53:55 AM: Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Hi. i came up with a similar q

Re: query - select from one, update another

2005-08-08 Thread SGreen
Sebastian <[EMAIL PROTECTED]> wrote on 08/08/2005 10:53:55 AM: > Gleb Paharenko wrote: > > >Hello. > > > > > > > >What about this: > > > > > > > >UPDATE news > > > >SET comments = > > > >( > > > > SELECT COUNT(id) > > > > FROM comments > > > > WHERE newsid = news.id > > > > GROUP BY news

Re: query - select from one, update another

2005-08-08 Thread Sebastian
Gleb Paharenko wrote: Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Hi. i came up with a similar query last night, but i didnt use group by. I have question, it it better to use COUNT

Re: query - select from one, update another

2005-08-08 Thread Gleb Paharenko
Hello. No, it isn't. Here is the test case: create table comments(id int auto_increment,newsid int,primary key(id)); create table news(id int,comments int); insert into news(id) values('1'),(2),(3),(4),(5); insert into comments(newsid) values(1),(1),(2),(4),(4),(4); update news set comm

Re: query - select from one, update another

2005-08-08 Thread Enrique Sanchez Vela
--- Gleb Paharenko <[EMAIL PROTECTED]> wrote: > Hello. > > What about this: > > UPDATE news > SET comments = > ( > SELECT COUNT(id) > FROM comments > WHERE newsid = news.id > GROUP BY newsid > ); > > Isn't the previous query going to update the news.comments wit

Re: query - select from one, update another

2005-08-08 Thread Gleb Paharenko
Hello. What about this: UPDATE news SET comments = ( SELECT COUNT(id) FROM comments WHERE newsid = news.id GROUP BY newsid ); Sebastian <[EMAIL PROTECTED]> wrote: > I have two tables: news and comments. > > i want to count the number of comments f

query - select from one, update another

2005-08-07 Thread Sebastian
I have two tables: news and comments. i want to count the number of comments for each newsid and update the count in news.comment comments.newsid belongs to news.id can i do this with sub queries? im using v4.1.x i have many records in the comments table so im not sure what the most efficien