Reply
Two Tables of information
Old 12-06-2007, 01:17 PM Two Tables of information
ChipJohns's Avatar
I don't know! Do you?

Posts: 477
Name: Chip Johns
Location: Savannah Georgia
Hi All, anyone help me with this one..!?

I have a page that displays information from a table something like a blog. It has small little articles on it.

Like this

-----------
Title
Paragraph or two
Comments

Title
Paragraph or two
Comments
---------------

"Comments" is a link they can click to view and add comments for that particular article. The comments are stored in another table. I was asked if I could show the number of current comments for each article,

i.e. Comments (2)

I am using a select to present the information with While Not and MoveNext to display all the articles.

What is the best way to list the amount of comments for each. I am supposing I will use a COUNT() but not quite sure if I will insert another Select statement or use a JOIN or what.

Any direction would be much appreciated.

Thanks,

Chip
ChipJohns is offline
Reply With Quote
View Public Profile Visit ChipJohns's homepage!
 
When You Register, These Ads Go Away!
     
Old 12-06-2007, 01:50 PM Re: Two Tables of information
chrishirst's Avatar
Super Moderator

Posts: 11,916
Location: Blackpool. UK
I do two queries for the one I'm coding, one to load the article/post, second to load the comments
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-06-2007, 10:34 PM Re: Two Tables of information
ChipJohns's Avatar
I don't know! Do you?

Posts: 477
Name: Chip Johns
Location: Savannah Georgia
Thanks Chris,

Do I nest the query for the comments inside of the while statement pulling the id from the main table's query?
Will this work?

Thanks!
ChipJohns is offline
Reply With Quote
View Public Profile Visit ChipJohns's homepage!
 
Old 12-07-2007, 12:38 AM Re: Two Tables of information
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Are you asking if you read in the list from your first query, and then fire off an individual query for each result row? Maybe instead, you can make a list of the ID values to look for, and use where id in ( 1, 2, 3, ... ) instead of nesting? That will be a more scalable approach, making less round trips to the database.

On that note, to get the number of comments, I'd do that with your asp code rather than sending down another query along the lines of select count(*) from x where id in ( 1, 2, 3, ... ).
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-07-2007, 06:18 AM Re: Two Tables of information
ChipJohns's Avatar
I don't know! Do you?

Posts: 477
Name: Chip Johns
Location: Savannah Georgia
Thanks Forrest,

I think I understand, use something like n+1 in a while loop ..?
What will happen if a row is deleted from the articles table? For instance the record where id=4 has been deleted. !! Is there a way to still make it work?

I appreciate the help.

Chip
ChipJohns is offline
Reply With Quote
View Public Profile Visit ChipJohns's homepage!
 
Old 12-07-2007, 01:50 PM Re: Two Tables of information
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,744
Name: John Alexander
Hmmm, deleted rows, do you have referential integrity set in your database? If you delete ID == 4 from your articles table, the database can then automatically delete all rows with ArticleID == 4 from your comments table.

If I'm understanding your question and your system, using declarative referential integrity will prevent the situation you're worried about coding for from happening in the first place. For programmers, laziness is a virtue.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 12-07-2007, 04:22 PM Re: Two Tables of information
ChipJohns's Avatar
I don't know! Do you?

Posts: 477
Name: Chip Johns
Location: Savannah Georgia
"declarative referential integrity "
Thanks, didn't know about this! very helpful..

I'm not sure I am completely understanding... Chris said that he uses 2 queries. I understand this but not really sure the best way to execute

I get confuesd soooo easily ... My concern was this:

ID-Name
01-Bob
02-Cindy
03-Stella
04-Mike
05-george

Delete a record:
ID-Name
01-Bob
02-Cindy
04-Mike
05-george

*Record three no longer exists. If I use a loop with n+1 I'm not sure how this is going to work.

Plus, I don't see how I can retrieve the number of records from table 2 without a query.

I'm thick I know... What am I not understanding?
ChipJohns is offline
Reply With Quote
View Public Profile Visit ChipJohns's homepage!
 
Old 12-07-2007, 09:18 PM Re: Two Tables of information
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Rereading the first message in this thread, try something like this pseudo-code:

Code:
Select
   *,
   (Select Count(*) From Comments Where ArticleID = Articles.ID) As CommmentCount
From
   Articles
The cascading deletes, or referential integrity is probably something worth looking into as well. Using a subquery, or another select statement as a column, will at least bring what you need back in a single round trip to the database.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-10-2007, 01:20 PM Re: Two Tables of information
ChipJohns's Avatar
I don't know! Do you?

Posts: 477
Name: Chip Johns
Location: Savannah Georgia
Worked perfect Forrest. Thanks..

I learned quite a bit with this. I'm glad I made the attempt. I appreciate everyone's help.
ChipJohns is offline
Reply With Quote
View Public Profile Visit ChipJohns's homepage!
 
Old 12-17-2007, 06:53 AM Re: Two Tables of information
Novice Talker

Posts: 12
Quote:
Originally Posted by ChipJohns View Post
Worked perfect Forrest. Thanks..

I learned quite a bit with this. I'm glad I made the attempt. I appreciate everyone's help.
All of this should be possible with one simple SQL query (Untested).

SELECT *, (SELECT count(*) FROM tbl_comments tc WHERE tc.article_id = ta.id) AS comments FROM tbl_articles ta
__________________
Sam M - Expert in Windows Web Hosting and Web Design and Development.
ServWise.com is offline
Reply With Quote
View Public Profile Visit ServWise.com's homepage!
 
Reply     « Reply to Two Tables of information
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15957 seconds with 13 queries