Go Back   Rhinocerus > Newsgroup > Newsgroup comp.databases.mysql

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 10:20 AM
Franz
Guest
 
Posts: n/a
Default difficult query...for me

Hi everybody,
I cannot figure out how to resolve this query that I am gonna describe
to you:

I have 2 tables:
CLUB
VISITS

CLUB contains many fields but the affected ones are:
idclub
number_of_visits

VISITS contains:
idvisit
type_of_visit
idclub

What I would like the query to do is:

list the CLUB table and in the "number_of_visit" field and put the COUNT
of visits for each "idclub" simply counting the rows of VISITS where
visits.idclub=club.idclub

Any suggestion about how to do it?

I did something like counting in a nested select...but i messed
everything up.

Thanks

Franz
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-26-2009, 10:34 AM
Captain Paralytic
Guest
 
Posts: n/a
Default Re: difficult query...for me

On 26 Nov, 11:20, Franz <f...@despammed.com> wrote:
> Hi everybody,
> I cannot figure out how to resolve this query that I am gonna describe
> to you:
>
> I have 2 tables:
> CLUB
> VISITS
>
> CLUB contains many fields but the affected ones are:
> idclub
> number_of_visits
>
> VISITS contains:
> idvisit
> type_of_visit
> idclub
>
> What I would like the query to do is:
>
> list the CLUB table and in the "number_of_visit" field and put the COUNT
> of visits for each "idclub" simply counting the rows of VISITS where
> visits.idclub=club.idclub
>
> Any suggestion about how to do it?
>
> I did something like counting in a nested select...but i messed
> everything up.
>
> Thanks
>
> Franz


If you have a VISITS table which contains 1 record for every visit,
then to find the number of visits for a club you just need to do a
COUNT & GROUP BY, whenever you want to find this information.

But what do you mean byut the COUNT of visits for each "idclub"?
Reply With Quote
  #3 (permalink)  
Old 11-26-2009, 11:08 AM
Robert Hairgrove
Guest
 
Posts: n/a
Default Re: difficult query...for me

Franz wrote:
> Hi everybody,
> I cannot figure out how to resolve this query that I am gonna describe
> to you:
>
> I have 2 tables:
> CLUB
> VISITS
>
> CLUB contains many fields but the affected ones are:
> idclub
> number_of_visits
>
> VISITS contains:
> idvisit
> type_of_visit
> idclub
>
> What I would like the query to do is:
>
> list the CLUB table and in the "number_of_visit" field and put the COUNT
> of visits for each "idclub" simply counting the rows of VISITS where
> visits.idclub=club.idclub
>
> Any suggestion about how to do it?
>
> I did something like counting in a nested select...but i messed
> everything up.
>
> Thanks
>
> Franz


It's not a good idea to store aggregate data (i.e. count of something)
in a column because every time a new visit occurs, you must update the
column. Data rapidly becomes inconsistent. You can always create a view
which shows you the data the way you describe (i.e. count of visits
together with the club id).

Someone else already pointed you in the right direction (hint: look up
"GROUP BY" in the SQL syntax part of the documentation).
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 11:15 AM
Franz
Guest
 
Posts: n/a
Default Re: difficult query...for me

Captain Paralytic ha scritto:

>
> If you have a VISITS table which contains 1 record for every visit,
> then to find the number of visits for a club you just need to do a
> COUNT & GROUP BY, whenever you want to find this information.
>
> But what do you mean byut the COUNT of visits for each "idclub"?


Hello Captain

I mistyped something
I meant:
list the CLUB table and put the COUNT of visits for each "idclub" simply
counting the rows of VISITS where visits.idclub=club.idclub in the
"number_of_visit" field

Franz
Reply With Quote
  #5 (permalink)  
Old 11-26-2009, 11:25 AM
Franz
Guest
 
Posts: n/a
Default Re: difficult query...for me

Robert Hairgrove ha scritto:

> It's not a good idea to store aggregate data (i.e. count of something)
> in a column because every time a new visit occurs, you must update the
> column. Data rapidly becomes inconsistent. You can always create a view
> which shows you the data the way you describe (i.e. count of visits
> together with the club id).
>
> Someone else already pointed you in the right direction (hint: look up
> "GROUP BY" in the SQL syntax part of the documentation).


thank you Robert,
I know how to group by with SQL, but this is not the case.
The visits I am talking about are not clicks on a web link, so the data
aren't so rapid

They're manual insertions of dates and other datas.

Any idea about how to fill the "number_of_visit" field as requested in
my previous post?

Thanks

Franz
Reply With Quote
  #6 (permalink)  
Old 11-26-2009, 11:44 AM
Captain Paralytic
Guest
 
Posts: n/a
Default Re: difficult query...for me

On 26 Nov, 12:25, Franz <f...@despammed.com> wrote:
> Robert Hairgrove ha scritto:
>
> > It's not a good idea to store aggregate data (i.e. count of something)
> > in a column because every time a new visit occurs, you must update the
> > column. Data rapidly becomes inconsistent. You can always create a view
> > which shows you the data the way you describe (i.e. count of visits
> > together with the club id).

>
> > Someone else already pointed you in the right direction (hint: look up
> > "GROUP BY" in the SQL syntax part of the documentation).

>
> thank you Robert,
> I know how to group by with SQL, but this is not the case.
> The visits I am talking about are not clicks on a web link, so the data
> aren't so rapid
>
> They're manual insertions of dates and other datas.
>
> Any idea about how to fill the "number_of_visit" field as requested in
> my previous post?
>
> Thanks
>
> Franz


The answers are still the same. It is still not a good idea and if you
really want to do it against all advice, the answer is still using
COUNT and GROUP BY.
Since you say that you "know how to group by with SQL", then what is
it that you do not know how to do in order to implement this bad idea?
Reply With Quote
  #7 (permalink)  
Old 11-26-2009, 11:50 AM
Robert Hairgrove
Guest
 
Posts: n/a
Default Re: difficult query...for me

Franz wrote:
> Any idea about how to fill the "number_of_visit" field as requested in
> my previous post?


UPDATE club C SET C.number_of_visits = (SELECT count(*) FROM visits V
WHERE V.idclub = C.idclub);

But it is not a good idea, as I stated in my first post, because the
actual number of visits can change in the meantime, and someone might
see old data.
Reply With Quote
  #8 (permalink)  
Old 11-26-2009, 12:04 PM
Franz
Guest
 
Posts: n/a
Default Re: difficult query...for me

Robert Hairgrove ha scritto:
> Franz wrote:
>> Any idea about how to fill the "number_of_visit" field as requested in
>> my previous post?

>
> UPDATE club C SET C.number_of_visits = (SELECT count(*) FROM visits V
> WHERE V.idclub = C.idclub);
>
> But it is not a good idea, as I stated in my first post, because the
> actual number of visits can change in the meantime, and someone might
> see old data.


I'm gonna try your solution, even if i cannot use the UPDATE statement.
I should use SELECT to list the resulting rows.
By the way, nobody can see these datas as only the admin has right to
list, change and add records to these tables.
So, if admin won't change anything, datas are persistent during all the
time elapsed between 2 insertions.

Thank you robert

Franz
Reply With Quote
  #9 (permalink)  
Old 11-26-2009, 12:29 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: difficult query...for me

Franz wrote:
> Robert Hairgrove ha scritto:
>> Franz wrote:
>>> Any idea about how to fill the "number_of_visit" field as requested
>>> in my previous post?

>>
>> UPDATE club C SET C.number_of_visits = (SELECT count(*) FROM visits V
>> WHERE V.idclub = C.idclub);
>>
>> But it is not a good idea, as I stated in my first post, because the
>> actual number of visits can change in the meantime, and someone might
>> see old data.

>
> I'm gonna try your solution, even if i cannot use the UPDATE statement.
> I should use SELECT to list the resulting rows.
> By the way, nobody can see these datas as only the admin has right to
> list, change and add records to these tables.
> So, if admin won't change anything, datas are persistent during all the
> time elapsed between 2 insertions.
>
> Thank you robert
>
> Franz


I agree with Robert. You should not be storing aggregate data in your
database. It has nothing to do with how rapidly the data changes or how
the data is input. It's all about maintaining consistency in your database.

As others have said - you can easily get the number of visits by using
COUNT and GROUP BY.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #10 (permalink)  
Old 11-26-2009, 12:30 PM
Captain Paralytic
Guest
 
Posts: n/a
Default Re: difficult query...for me

On 26 Nov, 13:04, Franz <f...@despammed.com> wrote:
> Robert Hairgrove ha scritto:
>
> > Franz wrote:
> >> Any idea about how to fill the "number_of_visit" field as requested in
> >> my previous post?

>
> > UPDATE club C SET C.number_of_visits = (SELECT count(*) FROM visits V
> > WHERE V.idclub = C.idclub);

>
> > But it is not a good idea, as I stated in my first post, because the
> > actual number of visits can change in the meantime, and someone might
> > see old data.

>
> I'm gonna try your solution, even if i cannot use the UPDATE statement.
> I should use SELECT to list the resulting rows.

This doesn't seem to make sense??? His solution is all about using the
UPDATE statement. However the question is still WHY would you want
this number_of_visits field at all when it is not needed. Or is your
speciality bad daabase design?

> By the way, nobody can see these datas as only the admin has right to
> list, change and add records to these tables.
> So, if admin won't change anything, datas are persistent during all the
> time elapsed between 2 insertions.

And this matters why?
Reply With Quote
  #11 (permalink)  
Old 11-26-2009, 12:30 PM
Robert Hairgrove
Guest
 
Posts: n/a
Default Re: difficult query...for me

Franz wrote:
> I'm gonna try your solution, even if i cannot use the UPDATE statement.
> I should use SELECT to list the resulting rows.


If you cannot do updates, how are you going to "try my solution"?

Try this instead:

SELECT C.idclub, (select count(*) from visits V where V.idclub=C.idclub)
as `number_of_visits` FROM club C;
Reply With Quote
  #12 (permalink)  
Old 11-26-2009, 12:47 PM
Captain Paralytic
Guest
 
Posts: n/a
Default Re: difficult query...for me

On 26 Nov, 13:30, Robert Hairgrove <rhairgr...@bigfoot.com> wrote:
> Franz wrote:
> > I'm gonna try your solution, even if i cannot use the UPDATE statement.
> > I should use SELECT to list the resulting rows.

>
> If you cannot do updates, how are you going to "try my solution"?
>
> Try this instead:
>
> SELECT C.idclub, (select count(*) from visits V where V.idclub=C.idclub)
> as `number_of_visits` FROM club C;


Isn't that all rather unnecessary?

SELECT
idclub,
COUNT(*)
FROM visits
GROUP BY idclub

Or, if you really want to include some data from the CLUB table
SELECT
c.idclub,
c.other_club_info,
COUNT(*)
FROM club c
JOIN visit v USING(idclub)
Reply With Quote
  #13 (permalink)  
Old 11-26-2009, 01:46 PM
Robert Hairgrove
Guest
 
Posts: n/a
Default Re: difficult query...for me

Captain Paralytic wrote:
> Isn't that all rather unnecessary?
>
> SELECT
> idclub,
> COUNT(*)
> FROM visits
> GROUP BY idclub
>
> Or, if you really want to include some data from the CLUB table
> SELECT
> c.idclub,
> c.other_club_info,
> COUNT(*)
> FROM club c
> JOIN visit v USING(idclub)


The first query won't give you any rows for clubs which haven't been
visited.
Reply With Quote
  #14 (permalink)  
Old 11-26-2009, 05:32 PM
Franz
Guest
 
Posts: n/a
Default Re: difficult query...for me

Captain Paralytic ha scritto:

> The answers are still the same. It is still not a good idea and if you
> really want to do it against all advice, the answer is still using
> COUNT and GROUP BY.
> Since you say that you "know how to group by with SQL", then what is
> it that you do not know how to do in order to implement this bad idea?


why beeing so caustic to me?
I have some knowledge about grouping in sql statements.
What I was asking for was a suggestion about how a field of a given
table can be filled with a sum coming from another table and bla bla bla..

Robert gave me a solution that is almost perfect.
That's all.
I don't know if the database I'm working on is bad designed. I cannot
change it.
It's not mine.
But I have to make some manipulations.
By the way, thank you too for your help. Next time I'll know how to NOT
design database.


Franz.
Reply With Quote
  #15 (permalink)  
Old 11-26-2009, 08:01 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: difficult query...for me

Franz wrote:
> Captain Paralytic ha scritto:
>
>> The answers are still the same. It is still not a good idea and if you
>> really want to do it against all advice, the answer is still using
>> COUNT and GROUP BY.
>> Since you say that you "know how to group by with SQL", then what is
>> it that you do not know how to do in order to implement this bad idea?

>
> why beeing so caustic to me?
> I have some knowledge about grouping in sql statements.
> What I was asking for was a suggestion about how a field of a given
> table can be filled with a sum coming from another table and bla bla bla..
>
> Robert gave me a solution that is almost perfect.
> That's all.
> I don't know if the database I'm working on is bad designed. I cannot
> change it.
> It's not mine.
> But I have to make some manipulations.
> By the way, thank you too for your help. Next time I'll know how to NOT
> design database.
>
>
> Franz.


You should know if it's designed well or not, and if it isn't, you
should be able to let your employer or client know, and why there are
problems. It's what competent programmers and designers do.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
 
Reply

Popular Tags in the Forum
difficult, queryfor

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
phpmyadmin query and browser query give different results TK Newsgroup comp.databases.mysql 12 11-16-2009 01:18 PM
RE: sysmaster query help Ian Michael Gumby Newsgroup comp.databases.informix 0 11-12-2009 05:10 PM
I need some help with Ruby query building and query time Alpha Blue Newsgroup comp.lang.ruby 3 07-28-2009 06:52 PM
Re: How to incorporate/assign variables within SQL query? Howard Schreier Newsgroup comp.soft-sys.sas 0 04-25-2008 03:04 AM
Re: How to incorporate/assign variables within SQL query? Irin later Newsgroup comp.soft-sys.sas 0 04-24-2008 07:32 PM



All times are GMT. The time now is 04:28 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.