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.
Author |
Topic |
naez
Starting Member
1 Post |
Posted - 2008-10-14 : 22:33:23
|
Okay so I've got a couple tables:articles-----------id - primarycat_id - INT index foreign #links up with categories tableauthor_id - INT index foreign # links up with user table title - varchar(255)body - longtexturl - varchar(255) # url-safe-tutorial-title-for-seo-links-----------categories-----------id - primarycategory - varchar(255)----------- The only thing is, I want to seperate things off by level too.So for instance:"Advanced" articles will carry say category id's 3,4,5"Intermediate" will carry 2"Novice" will carry cat_id 1with room for expansion of course.Now I know a couple ways to do this... add a new table, or add a parent_id field in categories. But I'm not sure the best way to do this so hralp!And more importantly, how to generate a big-ass query that will do all the following:Novice: - Novice Category 1 (2 articles)Intermediate - Intermediate Category 1 (5 articles) - Intermediate Category 2 (2 articles) - Intermediate Category 3 (20 articles)Advanced: - Advanced Category 1 (2 articles)# I know I'll need some semi-intricate joins and groupings, and a COUNT() I'm not exactly the SQL JOINs master so hopefully I can get some hralp.Also on side note should I use longtext or something else for the body?Thanks! |
|
jose_piratilla
Starting Member
7 Posts |
Posted - 2008-10-15 : 01:04:40
|
I don't like to use meaningful values on the ID field. Do you plan to change your categories tree?If not I would use something like thiscategories----------id -> Only ID number, no meaning.CatNumber -> 1,2,3Level -> "Advanced", "Intermediate", "Novice"The SQL Query would be something likeSELECT categories.Level, " - " & categories.Level & " Category " & Trim(Str(categorties.CatNumber)) AS CatName, COUNT(articles.Id) As ArtNumFROM categories JOIN articles ON categories.id=articles.id_catGROUP BY categories.id, categories.Level, categories.CatNumberORDER BY categories.level DESC, CatName;But if the categories can be changed I would usecategories----------id -> Only ID number, no meaning.CatNumber -> 1,2,3id_levellevels------idLevelNameLevelOrderAnd the query would be:SELECT levels.LevelName, " - " & levels.LevelName & " Category " & Trim(Str(categorties.CatNumber)) AS CatName, COUNT(articles.Id) As ArtNumFROM (categories JOIN levels ON categories.id_level = levels.id) JOIN articles ON categories.id=articles.id_catGROUP BY categories.id, levels.LevelName, categories.CatNumber, levels.LevelOrderORDER BY levels.LevelOrder, CatName; |
|
|
|
|
|
|
|