Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
dearI have 2 tables with name category and blogs,category contains catid, cat_namewhere blogs contains cat_name, blognamei have a lot of rows in blogs table with different different cat_namenow i want cat_name from category table and total rows for that cat_name from blogs tableplease help meregards,ASIF
Q
Yak Posting Veteran
76 Posts
Posted - 2006-06-29 : 10:33:30
Why using the table category when you already have cat_name in the table blogs???
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-06-29 : 10:37:33
Try this code
SELECT Category.Cat_Name, COUNT(*)FROM CategoryLEFT JOIN Blogs ON Blogs.Cat_Name = Category.Cat_NameGROUP BY Category.Cat_NameORDER BY Category.Cat_Name
If your tables were normalized, you could use this instead
SELECT Category.Cat_Name, COUNT(*)FROM CategoryLEFT JOIN Blogs ON Blogs.CatID = Category.CatIDGROUP BY Category.Cat_NameORDER BY Category.Cat_Name
Peter LarssonHelsingborg, Sweden
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-06-29 : 10:38:12
quote:Originally posted by Q Why using the table category when you already have cat_name in the table blogs???
I think this is because some categories are not used in blogs table...Peter LarssonHelsingborg, Sweden
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-06-30 : 01:07:20
To be really, really sure all categories are fetched, use this code (if there is not referential integrity between the tables)
SELECT z.Cat_Name, COUNT(*)FROM ( SELECT Cat_Name FROM Category UNION SELECT Cat_Name FROM Blogs )zLEFT JOIN Blogs ON Blogs.Cat_Name = z.Cat_NameGROUP BY z.Cat_NameORDER BY z.Cat_Name