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

  • Home
  • SEARCH
  • 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 6209981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:04:51+00:00 2026-05-24T06:04:51+00:00

I’m having a problem which I easily solve with php and some queries, but

  • 0

I’m having a problem which I easily solve with php and some queries, but I wanted to make one big query (at least if it’s faster which I believe it should be).

So I have 3 tables and let simplify them some:

topic which has 3 columns

  • id
  • user_id
  • visibility

access which has 2 columns

  • topic_id
  • user_id

friendship which has 2 columns

  • user_id
  • friend_id

What I want to do is if a user (we call him watch_id) is trying to watch this topic I want to return the topic if he is allowed to watch or if he is not, return nothing.
He is allowed to watch if any of these is true:

  • watch_id == user_id
  • visibility == 3
  • visiblity == 2 && the friendship table returns a row when friendship.user_id = topic.user_id && friendship.friend_id == watch_id
  • visibility == 1 && the access table returns a row when access.topic_id = topic.id && access.user_id == watch_id

As you can see it’s not very hard to do with php and a bunch of queries but in sql only I can’t figure it out. I’ve been trying joins, case and stuff but the logic never adds up. 🙁

So can you guys help me? Or am I stuck with php and lots of queries?

Edit: Hmm, looks like I wasn’t making myself perfectly clear, my bad! He is allowed to watch if any of those requirements is true. Since this is the case I go with left join answer which was easiest to extend. Thanks all!

  • 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-24T06:04:52+00:00Added an answer on May 24, 2026 at 6:04 am

    You should be able to make left joins against the friendship and access tables, so that the joins themselves doesn’t limit the result:

    select something
    from topic
    left join friendship on friendship.user_id = topic.user_id && friendship.friend_id == watch_id
    left join access on access.topic_id = topic.id && access.user_id == watch_id
    where
      watch_id == topic.user_id or
      topic.visibility == 3 or
      (topic.visiblity == 2 and friendship.user_id is not null) or
      (visibility == 1 && access.topic_id is not null)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.