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 for

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 with the last

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

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

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 newsid ); Hi. i came up with a similar

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

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

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 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 this: UPDATE news SET comments = (

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
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. What about this: UPDATE news SET

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

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.

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.

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