Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2003, 04:28 PM
Authorized User
 
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recordset Problems...

I feel this should be easy, but I have been having a hell of a time with it.

I want to return a recordset (using T-SQL) that displays certain records in a table, with the last column displaying a count pulled from another table, using a key in its WHERE clause...

Something like:

SELECT
a.articleid,
a.title,
(select count(*) from blog_comments where ???)
FROM blog_articles a
INNER JOIN blog_comments c
ON a.articleid=c.articleid

...but where the third select parameter (select count(*) from blog_comments where ???)
returns the number of rows in the blog_comments table that match the articleid of the the each row in the blog_articles table.

So the resulting recordset would be something like:

10003 Fun with Books 6
10004 Fun with Kites 3
10005 Fun with Bikes 1

The 6,3, and 1 would be the number of rows in the blog_comments table with a matching articleid to 10003,1004, and 10005.

I have tried using cursors, but find that it returns multiple recordsets that aren't going to work in the upper layers of my app. And then there is the performance hit...

If I am not making any sense, please let me know...

Thanks, folks...

Ben


 
Old October 16th, 2003, 04:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

If I understand you correctly, I think this will do it:

Code:
SELECT 
a.articleid,
a.title, 
(select count(*) from blog_comments c where c.articleid=a.articleid)
FROM blog_articles a
 
Old October 16th, 2003, 05:22 PM
Authorized User
 
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Holy over-complicating it, batman!

Thanks a lot, that did it...don't know what I was thinking there.

Ben






Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordset Problems toosuave PHP Databases 1 August 27th, 2006 09:03 PM
DAO Recordset Problems SteveH Access VBA 1 October 5th, 2005 05:39 PM
recordset problems p2ptolu Classic ASP Databases 0 July 9th, 2005 07:54 AM
Problems importing Excel into ADO.Net recordset AshleyC ADO.NET 0 February 22nd, 2005 05:56 AM
Recordset Problems Night_Zero Classic ASP Databases 17 July 15th, 2004 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.