Connecting Tech Pros Worldwide Help | Site Map

mysql question and a really bad joke

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:19 AM
Alexander Ross
Guest
 
Posts: n/a
Default mysql question and a really bad joke

Lets say I have 2 tables, tbl_topics and tbl_lessons:


tbl_topics

topicID
INT

parentID
INT

Title
VARCHAR



tbl_lessons

lessonID
INT

topicID
INT (FK)

Other fields




I have a query that gives me all the topics who's parentID = $whatParentID
:

"SELECT topicID FROM tbl_topics WHERE parentTopicID=$whatTopicID"


Now I want to modify this query so I get a second column called count that
contains the number of times the returned topicID is found in tbl_lessons
even if that count is 0.


Example result:

topicID
Count

1000
2

1003
0

1021
4



In this example, topics 1000,1003,and 1021 all have a parentID of 1009 (or
whatever) and 1000 has 2 lessons, 1003 has 0, and 1021 has 4.


Anyone? Anyone? Bueller? Bueller?



BAD JOKE: (read this outloud)

Have you heard about circus sex? its in tents.





  #2  
Old July 17th, 2005, 04:19 AM
torjon@NOSPAMnterport.net
Guest
 
Posts: n/a
Default Re: mysql question and a really bad joke

On Sun, 21 Mar 2004 02:38:01 GMT, "Alexander Ross"
<alexross@bleen.net> wrote:
[color=blue]
>Lets say I have 2 tables, tbl_topics and tbl_lessons:
>
>
>tbl_topics
>
> topicID
> INT
>
> parentID
> INT
>
> Title
> VARCHAR
>
>
>
>tbl_lessons
>
> lessonID
> INT
>
> topicID
> INT (FK)
>
> Other fields
>
>
>
>
>I have a query that gives me all the topics who's parentID = $whatParentID
>:
>
>"SELECT topicID FROM tbl_topics WHERE parentTopicID=$whatTopicID"
>
>
>Now I want to modify this query so I get a second column called count that
>contains the number of times the returned topicID is found in tbl_lessons
>even if that count is 0.
>
>
>Example result:
>
> topicID
> Count
>
> 1000
> 2
>
> 1003
> 0
>
> 1021
> 4
>
>
>
>In this example, topics 1000,1003,and 1021 all have a parentID of 1009 (or
>whatever) and 1000 has 2 lessons, 1003 has 0, and 1021 has 4.
>
>
>Anyone? Anyone? Bueller? Bueller?
>[color=green]
>>"[/color]
>
>BAD JOKE: (read this outloud)
>
>Have you heard about circus sex? its in tents.[/color]

SELECT t.topicID, c.count FROM tbl_topics t inner join (select
topicID, count(1) from tbllessons group by topicID) c on c.topic_id =
t.topic_id WHERE t.parentTopicID=$whatTopicID"[color=blue]
>
>
>
>[/color]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.