473,395 Members | 1,452 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,395 software developers and data experts.

2 left joins of the same table

dan
i have 2 tables that i want to count

TABLE 1
categories
--id
--name

forum_comments
--id
--category_id
--index (0 for top topic, or comment ID for sub posts)
--title
--comment

I'm trying to list all of the categories w/ a topic count and a posts
count, so I'm trying to join a select of categories with count()s. my
joins are of the same table but they keep coming up the same number?

select
C.id ,
C.name,
count(F.id) as topics,
count(F2.id) as posts
FROM categories as C
LEFT OUTER
JOIN forum_topics as F ON C.id=F.category_id AND F.level=0
LEFT OUTER
JOIN forum_topics as F2 ON C.id=F2.category_id
GROUP BY C.id

Any ideas?
thanks
Jul 20 '05 #1
1 4610
dan
well, no one posted but i found out what i was doing wrong. this id
for anyone who finds this in the future. I just had to ad DISTINCT
clause in my count.

select
C.id ,
C.name,
count(DISTINCT F.id) as topics,
count(DISTINCT F2.id) as posts
FROM categories as C
LEFT
JOIN forum_topics as F ON C.id=F.category_id AND F.level=0
LEFT
JOIN forum_topics as F2 ON C.id=F2.category_id
GROUP BY C.id


ry*****@yahoo.com (dan) wrote in message news:<7b**************************@posting.google. com>...
i have 2 tables that i want to count

TABLE 1
categories
--id
--name

forum_comments
--id
--category_id
--index (0 for top topic, or comment ID for sub posts)
--title
--comment

I'm trying to list all of the categories w/ a topic count and a posts
count, so I'm trying to join a select of categories with count()s. my
joins are of the same table but they keep coming up the same number?

select
C.id ,
C.name,
count(F.id) as topics,
count(F2.id) as posts
FROM categories as C
LEFT OUTER
JOIN forum_topics as F ON C.id=F.category_id AND F.level=0
LEFT OUTER
JOIN forum_topics as F2 ON C.id=F2.category_id
GROUP BY C.id

Any ideas?
thanks

Jul 20 '05 #2

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

Similar topics

0
by: Jaz | last post by:
Hello, Could do with a bit of advice regarding left joins, would be much appreciated! OK, I have a table of products. I wish to have another table of images (filenames only) and a third table...
5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
1
by: CstmrSrvc | last post by:
I'm not sure if this is the right forum, so please forgive me if I"m off. I'm doing a query from MS Excel using the Query Wizard. I'm pulling from 4 different JDE Tables. I need to join AND...
3
by: nico3334 | last post by:
I currently have a query that Joins 2 Tables (Table1 and Table2) using LEFT OUTER JOIN. Here is an example of that query: SELECT a.supply, a.state, b.cost FROM Table1 a LEFT...
1
by: nico3334 | last post by:
I have a query that currently pulls data from a main table and a second table using LEFT OUTER JOIN. I know how to do make another LEFT OUTER JOIN with the main table, but I want to add another LEFT...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.