473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Very dificult (advanced) query

Greetings!

Can someone please help me to crack this problem? I have 4 tables:
Subject, Forum, Topic and Post. A Subject groups various Forums, a
Forum groups various Topics, and a Topic groups several Posts. The
references inside the tables are:

+-----------+ +-----------+ +-----------+ +-----------+
| Subject | | Forum | | Topic | | Post |
+-----------+ +-----------+ +-----------+ +-----------+
| idsubject | | idforum | | idtopic | | idpost |
| idsubject | | idforum | | idtopic |

The query I was doing returned all the Forums grouped by Subjects.
Having this resultset I would then iterate through it (in a PHP
script) and for each row (i.e. Forum) I would then perform 2 other
queries: one to get the number of Topics in that Forum and another to
get the number of Posts in that Topic. This is, obviously, not the
best way of doing it - if I had 10 topics, I would end up querying the
database 21 times in one single PHP script... :-( This were the
original queries:

SELECT Subject.idsubject, Forum.idforum
FROM Subject, Forum
WHERE Subject.idsubject=Forum.idsubject
ORDER BY Subject.idsubject, Forum.idforum

Then for each row of this resultset I do these:

SELECT COUNT(idtopic)
FROM Topic
WHERE idforum=(script fills current idforum here)

SELECT COUNT(idpost)
FROM Post, Topic
WHERE Topic.idtopic=Post.idtopic
AND Topic.idforum=(script fills current idforum here)
I'm now try to optimize all the querying stuff in my website and I
would like to do this specific section in only one single query. I
achieved to get as far as returning all the Forums grouped by Subjects
with the respective number of Topics for each (i.e. I managed to have
the 2 first queries in only one). This is the new query:

SELECT Subject.idsubject, Forum.idforum, count(idtopic)
FROM Subject, Forum LEFT JOIN Topic ON Topic.idforum=Forum.idforum
WHERE Subject.idsubject=Forum.idforum
GROUP BY Subject.idsubject, Forum.idforum

But I can't seem to group the 3 queries in only one though... I can't
find a way to also return the total number of Posts for each Topic. I
tried:

SELECT Subject.idsubject, Forum.idforum, count(Topic.idtopic),
count(Post.idpost)
FROM Subject, Post LEFT JOIN (Forum LEFT JOIN Topic ON
Topic.idforum=Forum.idforum) ON Topic.idtopic=Post.idtopic
WHERE Subject.idsubject=Forum.idsubject
GROUP BY Subject.idsubject, Forum.idforum

I tried this one and many other, but I'm really just shooting in the
dark here... Again, what I wanted was to return a resultset with all
the Forums and their respective Subjects, together with the total
number of Topics for each Forum and the total number of Posts for each
Topic.

Is this even doable? I think so... Anyone care to help please?
Best regards.
Pedro Fonseca, from Portugal
Jul 20 '05 #1
6 1376
Pedro Fonseca wrote:
Again, what I wanted was to return a resultset with all
the Forums and their respective Subjects, together with the total
number of Topics for each Forum and the total number of Posts for each
Topic.

Is this even doable? I think so... Anyone care to help please?


Olá, Pedro!

In my estimation this is not doable in one query (except for a UNION of
two incongruous queries).

To get the count of posts per topic, you would need one row in the
result set for each distinct topic. But you are aggregating the topics,
so the result set has zero, one or many topics represented in each row.

I suggest that you consider it a victory to have reduced 21 queries to
2, and move on to your next task. :-)

Regards,
Bill K.
Jul 20 '05 #2
Bill Karwin <bi**@karwin.com> wrote in message news:<cg********@enews2.newsguy.com>...
Pedro Fonseca wrote:
Again, what I wanted was to return a resultset with all
the Forums and their respective Subjects, together with the total
number of Topics for each Forum and the total number of Posts for each
Topic.

Is this even doable? I think so... Anyone care to help please?


Olá, Pedro!

In my estimation this is not doable in one query (except for a UNION of
two incongruous queries).

To get the count of posts per topic, you would need one row in the
result set for each distinct topic. But you are aggregating the topics,
so the result set has zero, one or many topics represented in each row.

I suggest that you consider it a victory to have reduced 21 queries to
2, and move on to your next task. :-)

Regards,
Bill K.


OK, I think I'll take your advise.
Obrigado Bill! ;-)
Jul 20 '05 #3
> > [Pedro Fonseca]
Again, what I wanted was to return a resultset with all
the Forums and their respective Subjects, together with the total
number of Topics for each Forum and the total number of Posts for each
Topic.


[Bill Karwin]
To get the count of posts per topic, you would need one row in the
result set for each distinct topic. But you are aggregating the topics,
so the result set has zero, one or many topics represented in each row.


Actually (I'm sorry, I really couldn't give up on this just yet :-),
what if I added a slight change to what I originally intended? What if
I was trying to have a result set with all the Forums and their
respective Subjects, together with the total number of Topics for each
Forum and the total number of Posts in that same Forum (note that it's
the grand total number of Posts for each Forum, not for each Topic). I
get the feeling that this is perhaps doable, but I could still be
wrong... Any help?
Jul 20 '05 #4
Pedro Fonseca wrote:
Actually (I'm sorry, I really couldn't give up on this just yet :-),
what if I added a slight change to what I originally intended? What if
I was trying to have a result set with all the Forums and their
respective Subjects, together with the total number of Topics for each
Forum and the total number of Posts in that same Forum (note that it's
the grand total number of Posts for each Forum, not for each Topic). I
get the feeling that this is perhaps doable, but I could still be
wrong... Any help?


This sounds more likely. Now you are counting the posts using the same
grouping criteria by which you grouped the count of topic id's, so this
creates no logical conflict.

But it depends on the MySQL behavior for GROUP BY, which I confess I
don't understand very well. In theory, the following query might work
in your example:

SELECT S.idsubject, F.idforum, COUNT(DISTINCT T.idtopic),
COUNT(DISTINCT P.idpost)
FROM Subject S INNER JOIN Forum F ON S.idsubject = F.idforum
LEFT JOIN Topic T ON T.idforum = F.idforum
LEFT JOIN Post P ON P.idtopic = T.idtopic
GROUP BY S.idsubject, F.idforum

My hesitation is that I get the idea that MySQL allows greater
flexibility with regards to computing multiple aggregates than the SQL
standard and some other RDBMS's generally allow. But I'm not sure
exactly how flexible it is. :-)

Regards,
Bill K.
Jul 20 '05 #5
no****@pedrofonseca.com (Pedro Fonseca) wrote in message news:<15*************************@posting.google.c om>...
[Pedro Fonseca]
Again, what I wanted was to return a resultset with all
the Forums and their respective Subjects, together with the total
number of Topics for each Forum and the total number of Posts for each
Topic.


[Bill Karwin]
To get the count of posts per topic, you would need one row in the
result set for each distinct topic. But you are aggregating the topics,
so the result set has zero, one or many topics represented in each row.


[Pedro Fonseca]
Actually (I'm sorry, I really couldn't give up on this just yet :-),
what if I added a slight change to what I originally intended? What if
I was trying to have a result set with all the Forums and their
respective Subjects, together with the total number of Topics for each
Forum and the total number of Posts in that same Forum (note that it's
the grand total number of Posts for each Forum, not for each Topic). I
get the feeling that this is perhaps doable, but I could still be
wrong... Any help?


It was hard but I came up with a solution to this problem - but many
thanks to anyone that might answer instead. For the sake of keeping
things for the record, for future reference, the query that allows
this is:

SELECT Subject.idsubject, Forum.idforum, count(DISTINCT
Topic.idtopic), count(DISTINCT idpost)
FROM Subject, (Forum LEFT JOIN Topic ON Forum.idforum=Topic.idforum)
LEFT JOIN Post ON Topic.idtopic=Post.idtopic
WHERE Subject.idsubject=Forum.idforum
GROUP BY Subject.idsubject, Forum.idforum

Again, this returns a result set with a row for each Forum and their
respective Subject, together with the total number of Topics for each
Forum as well as the total number of Posts for each Forum (not for
each Topic as mentioned earlier). Should a Forum not have a Topic yet,
0 is returned as the count of the Topics, and should an existing Topic
not have any Posts yet, 0 is also returned as the count of the Posts.
This way, every single Forum is returned, even if they still don't
have any Topics or Posts.

I'm moving on to the next problem now, but do you think this query can
be further optimized?

Best regards!
Jul 20 '05 #6
Bill Karwin <bi**@karwin.com> wrote in message news:<cg********@enews3.newsguy.com>...
Pedro Fonseca wrote:
Actually (I'm sorry, I really couldn't give up on this just yet :-),
what if I added a slight change to what I originally intended? What if
I was trying to have a result set with all the Forums and their
respective Subjects, together with the total number of Topics for each
Forum and the total number of Posts in that same Forum (note that it's
the grand total number of Posts for each Forum, not for each Topic). I
get the feeling that this is perhaps doable, but I could still be
wrong... Any help?


This sounds more likely. Now you are counting the posts using the same
grouping criteria by which you grouped the count of topic id's, so this
creates no logical conflict.

But it depends on the MySQL behavior for GROUP BY, which I confess I
don't understand very well. In theory, the following query might work
in your example:

SELECT S.idsubject, F.idforum, COUNT(DISTINCT T.idtopic),
COUNT(DISTINCT P.idpost)
FROM Subject S INNER JOIN Forum F ON S.idsubject = F.idforum
LEFT JOIN Topic T ON T.idforum = F.idforum
LEFT JOIN Post P ON P.idtopic = T.idtopic
GROUP BY S.idsubject, F.idforum

My hesitation is that I get the idea that MySQL allows greater
flexibility with regards to computing multiple aggregates than the SQL
standard and some other RDBMS's generally allow. But I'm not sure
exactly how flexible it is. :-)

Regards,
Bill K.


Indeed, that's perfect. ;-) We came up with pretty much the same
solution (I'm posting to this newsgroup through Google, so it takes
several hours before my post appears. When I posted my solution yours
wasn't here, sorry...). I'm also not that much aware myself of the
quircks offered by MySQL to optimize this sort of query. Eventually
I'll be giving that a look, but if anyone can suggest something just
by looking at the query, please do it. Thanks for being there Bill!

Best regards!
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pedro Fonseca | last post by:
Greetings! Can someone please help me to crack this problem? I have 4 tables: Subject, Forum, Topic and Post. A Subject groups various Forums, a Forum groups various Topics, and a Topic groups...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
4
by: Nhmiller | last post by:
This is directly from Access' Help: "About designing a query When you open a query in Design view, or open a form, report, or datasheet and show the Advanced Filter/Sort window (Advanced...
3
by: Mario Soto | last post by:
Hi. i hava a postresql 7.4.2 in a production server. tha machine is a Pentium IV 2,6 GHZ AND 1 GB IN RAM with lINUX RH 9.0. The postresql.conf say: ...
3
by: | last post by:
I'm planning to transport a desktop application to the web. A spin-off of this application has already been put on the web by another programmer. He used ColdFusion with MS SQL, Access, VC, and...
0
by: Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post by:
I am running Access 2007. I have a report that I want to filter. I can go into Advanced...Advanced Filter/Sort... and setup a filter that works fine on the report when I apply it. When in this...
0
by: Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post by:
I am running Access 2007. I have a report that I want to filter. I can go into Advanced...Advanced Filter/Sort... and setup a filter that works fine on the report when I apply it. When in this...
6
by: MLH | last post by:
I have a small bitmap graphic on a report. I would like to set it's Visible property to True whe the value of a certain field on the report is True. Can I do that? I have tried in the OnFormat...
1
by: mbatestblrock | last post by:
I think I have a rather advanced question that I was hoping to find some good help with. I am still pretty new to VBA and I know that doesn't help my situation here. But here is what I am trying to...
2
by: rajendrsedhain | last post by:
Hi, I have 1 checkboxlist, 5 dropdownlits and three textboxes.I have to write the SQL query and c# code for that advanced search. <asp:CheckBoxList ID="reposotoryCheckBoxList" runat="server"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.