Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:49:20+00:00 2026-05-23T06:49:20+00:00

So I have an SQL Table structure such that I have Blogs which have

  • 0

So I have an SQL Table structure such that I have

  1. Blogs which have blogIDs
  2. BlogAffiliates which join with Blogs on BlogID [Gets people affiliated
    with that Blog]
  3. BlogPosts which join with Blogs on BlogID [Gets all posts for a blog]
  4. BlogPostPictures which join with BlogPosts on BlogPostID [Gets pictures for all posts for a given blog]

So I supply a username to match with BlogAffiliates which should get me all the BlogIDs associated with that user, which in turn gets me all blogs, which in turn gets me all blog posts, which in turn gets me all blogpost pictures.

This works fine. My problem is BlogPosts also have BlogPostTags which use a BlogPostID to match to a table of BlogPostTags.

I have the following query

    select * from BlogPosts,BlogAffiliates, Blogs, BlogPostPictures
      where BlogAffiliates.UserID = @UserID 
      and BlogPosts.BlogID = BlogAffiliates.BlogID
      and Blogs.BlogID = BlogAffiliates.BlogID and 
      BlogPosts.BlogPostID = BlogPostPictures.BlogPostID

This will get me a row of data including all the info I need for EACH blog post for a given user. How would I include the tags on this? The problem is I have many BlogPostTags for each BlogPost so I have no idea how to join or group this info ??
BlogPostTags has a BlogPotID which could join with BlogPosts. Would I need to return a copy of EACH row once for each tag including that tag? That seems like an explosion of data, whereas now 10 BlogPosts would return 10 rows if they each had 5 BlogPostTags that would be 50 rows?? Have I structured my tables poorly or am I missing some way to aggregate the tags into each row [I dont even care if they aggregate into 1 field and I delimit them or something and decode it on the other end of the query]

Thanks in advance!

Any ideas???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:49:20+00:00Added an answer on May 23, 2026 at 6:49 am

    Well it kind’a depends on what you want to do with the data.

    Your table structure is fine.

    If you want the same number of rows returned as current – i.e. 1 per affiliate per blogpost (assuming 1 per picture per blogpost?) then you’ll need to package up the tags into a single column in the resultset.

    Alternatively you could simply manage the full resultset in code:

    For example you could have a BlogPost object which contained a collection of Tags – An ORM would handle this for you easily – alternatively use a datareader with a loop and iterate through building up the tag collection, moving onto the next record when the blogpostid changes.

    In SQL – you could write a UDF to perform the same function.

    CREATE FUNCTION [dbo].[GetTagsByBlogPostId] (
    @BlogPostID int
    )
    RETURNS varchar(max)
    AS
    DECLARE @Tags @OUTPUT varchar(max)
    SELECT @Tags = COALESCE(@Tags + ', ', '') + Tag
    FROM BlogPostTags
    where BlogPostId = @BlogPostId
    
    RETURN @Tags END
    

    Then in your SQL Statement above just append a call to the UDF to the SELECT statement:

     select * from BlogPosts,BlogAffiliates, Blogs, BlogPostPictures, dbo.GetTagsByBlogPostId(BlogPosts.BlogPostId)
      where BlogAffiliates.UserID = @UserID 
      and BlogPosts.BlogID = BlogAffiliates.BlogID
      and Blogs.BlogID = BlogAffiliates.BlogID and 
      BlogPosts.BlogPostID = BlogPostPictures.BlogPostID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL server that has the normal tree structure of
I have a 3 table structure such that: Table Patient - PatientID PK has
I have to write a component that re-creates SQL Server tables (structure and data)
I have a SQL table which has a number of fields ID | Value
I have a sql table which have the following data, Id City Country ---
I have a T-SQL table variable (not a table) which has an auto incrementing
I have a MS SQL table that I don't have any control over and
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
I have an SQL table with the following structure: Code1 - int Code2 -
I have below table structure in MS SQL AirQuoteID Name SalesValue 7 M 49.50

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.