Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting Data from two tables

Author  Topic 

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-07-23 : 17:04:00
Hi,

I have a Categories Table and an Article Table.

Categories Table Schema:
CategoryID
CategoryName

Articles Table Schema:
ArticleID
ArticleName
Description
CategoryID


Now I want to select so I can show my records like this:

CategoryName1
All Articles in this Category1

CategoryName2
All Articles in this Category2

I think you get the idea:

Here is my query but Its returning CategoryName many number of times because There are more articles than category.

SELECT c.Title,a.Title,a.ArticleID FROM Articles a,Categories c
WHERE c.CategoryID = a.CategoryID AND Active = 1
GROUP BY c.Title,a.Title,a.ArticleID

Thanks,


Mohammad Azam
www.azamsharp.net

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2005-07-24 : 19:36:40
It is possible to do, but surely it's easier to do this in the display layer?

SELECT C.DisplayName FROM
(SELECT CategoryID, 0 as Seq, CategoryName as DisplayName
FROM Categories INNER JOIN Articles ON A.CategoryID = C.CategoryID
UNION
SELECT CategoryID, 1 as Seq, ArticleName as DisplayName
FROM Articles) C
ORDER BY CategoryID, seq


Go to Top of Page
   

- Advertisement -