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 7954545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:27:37+00:00 2026-06-04T03:27:37+00:00

I have 3 tables – posts, friendships, and likes — the important fields described

  • 0

I have 3 tables – posts, friendships, and likes — the important fields described below.

posts: id, user_id, title

friendships: requester_id, accepter_id, is_confirmed

likes: post_id, user_id

I have a method called get_relevant_posts() which should return posts that either I’ve written, my confirmed friends have written, or my confirmed friends have liked. I’ve already implemented this in SQLAlchemy, but there’s a bug in my query because it only works if the friendships table is not empty (doesnt matter whats in it, just cant be empty). If it is empty, nothing gets returned (improper join?).

This is not a big issue as the friendships table will have something in it 99.99% of the time. But I still want to take a step back and write this in plain SQL — I just can’t seem to wrap my head around it.

SQLALCHEMY CODE

Friendship1 = aliased(Friendship)
Friendship2 = aliased(Friendship)
Friendship3 = aliased(Friendship)
Friendship4 = aliased(Friendship)
return (DBSession.query(Post)
        .outerjoin(Friendship1, Post.user_id==Friendship1.accepter_id)
        .outerjoin(Friendship2, Post.user_id==Friendship2.requester_id)
        .outerjoin(Like, Post.id==Like.post_id)
        .filter(or_(
            # My post
            self.id==Post.user_id,
            # My friends post, where I'm the requester of the friendship
            and_(self.id==Friendship1.requester_id,
                 Friendship1.is_confirmed==True),
            # My friends post, where I'm the accepter of the friendship
            and_(self.id==Friendship2.accepter_id,
                 Friendship2.is_confirmed==True),
            # Somebodies post, which my friends found interesting
            and_(self.id==Friendship3.requester_id,
                 Friendship3.is_confirmed==True,
                 Like.user_id==Friendship3.accepter_id),
            and_(self.id==Friendship4.accepter_id,
                 Friendship4.is_confirmed==True,
                 Like.user_id==Friendship4.requester_id)))
        .order_by(Post.created_at.desc())
        .limit(limit)
        .offset(offset)
        .all())

PRODUCES THIS

SELECT posts.title AS posts_title
FROM friendships AS friendships_1, friendships AS friendships_2, posts LEFT OUTER JOIN friendships AS friendships_3 ON posts.user_id = friendships_3.accepter_id LEFT OUTER JOIN friendships AS friendships_4 ON posts.user_id = friendships_4.requester_id LEFT OUTER JOIN likes ON posts.id = likes.post_id LEFT OUTER JOIN users AS users_1 ON users_1.id = posts.user_id 
WHERE (posts.user_id = %s OR friendships_3.requester_id = %s AND friendships_3.is_confirmed = %s OR friendships_4.accepter_id = %s AND friendships_4.is_confirmed = %s OR friendships_1.requester_id = %s AND friendships_1.is_confirmed = %s AND likes.user_id = friendships_1.accepter_id OR friendships_2.accepter_id = %s AND friendships_2.is_confirmed = %s AND likes.user_id = friendships_2.requester_id) ORDER BY posts.created_at DESC 
LIMIT %s
  • 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-06-04T03:27:38+00:00Added an answer on June 4, 2026 at 3:27 am

    Assuming we have MS SQL 2005

    With rels
    (
       Select f.requesterId as target_id
       From friendships f 
       Where is_confirmed = 1 and f.accepter_id='Your Id'
       Union 
       Select 'Your id'
    )
    Select p.*
    From posts p 
      left join rel r on r.target_id = p.user_id  -- this will be null on for non-target users
      left join likes l on p.post_id=l.post_id -- here we will pick likes
      left join rel r1 on r1.target_id=l.user_id
    where coalesce(r1.target_id, r2.target_id) is not null -- telling sql to pick posts we are interested in
    

    or replace CTE rel with sub-query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
I have tables (for simplicity sake) as outlined below: Category -------------------- CategoryId (0 for
I have tables A and B and A_has_B where have this fields: A: id,
Suppose i have tables Products -------product_id , name , price , size shopping_cart------cart_id,item_id,user_id,quantity order----order_id
I have tables as below table A emp_code | emp_name table B emp_code |
I have tables: users with the fields id, premium votes with the fields userid,
I have tables containing the following fields among others in a application for a
I have tables: user, comment and post, say: A user can have many posts
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity

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.