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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:06:20+00:00 2026-06-04T02:06:20+00:00

I am designing a site like pinterest ( for study ) and I want

  • 0

I am designing a site like pinterest ( for study ) and I want to know what query I have to do at the homepage to show the user’s stream.

I have created these 6 tables:

users
boards
pictures
boards_pictures ( many-to-many table )
followers
comments

And in home, I do this query to get all followers’ pictures.

SELECT users.username, pictures.link, comments.comment, boards.title
FROM boards_pictures, pictures, followers, users, comments, boards 
WHERE ( boards_pictures.id_user = followers.id_following ) 
  AND ( boards_pictures.id_picture = pictures.id ) 
  AND ( followers.id_user = $_session['userid'] ) 
  AND ( users.id = followers.id_following )
  AND ( comments.picture_id = pictures.id )
  AND ( boards.id = boards_pictures.boards_id )

Is there a way to avoid this complex query ( a JOIN with 6 tables ) ?

  • 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-04T02:06:21+00:00Added an answer on June 4, 2026 at 2:06 am

    Here’re the DDL statements deduced from your query:

    CREATE TABLE users (id integer, username varchar(30));
    CREATE TABLE boards (id integer, title varchar(30));
    CREATE TABLE pictures (id integer, link varchar(90));
    CREATE TABLE boards_pictures (
      id_user integer,
      id_picture integer,
      boards_id integer);
    CREATE TABLE followers (id_user integer, id_following integer);
    CREATE TABLE comments (picture_id integer, comment varchar(350));
    

    You’re mixing column naming styles here, compare users.id, followers.id_user, comments.picture_id and board_pictures.id_picture (last 2 are quite misleading). The more tables you’ll have, the more attention you’ll have to pay on how column is named. It’s better to stick to a single common pattern, picture_id or user_id being most appropriate in my view.

    Your query is not complicated, except you’re using implicit join notation. It is not the recommended way, as there’s a chance to miss some predicates and end up with Cartesian product of 2 (or more) tables.

    Your query can be rewritten like this:

    SELECT u.username, p.link, c.comment, b.title
      FROM boards_pictures bp
      JOIN pictures p ON p.id = bp.id_picture
      JOIN followers f ON bp.id_user = f.id_following
      JOIN users u ON u.id = f.id_following
      JOIN comments c ON c.picture_id = p.id
      JOIN boards b ON b.id = bp.boards_id
    WHERE f.id_user = $_session['userid'];
    

    As you see now, query is really simple and has only one predicate.
    I’ve created a testbed without data on SQL Fiddle.

    In my view your structure is quite good. There’s no need to change neither table design nor this query.

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

Sidebar

Related Questions

i am designing a site with a comment system and i would like a
I'm designing a site. It is in a very early stage, and I have
I'm re-designing a Web site and I have a problem with the existing data
I'm designing an HTML 5 site using a header, article, and footer. I have
I'm designing a fairly simple web site but as I don't have much experience
I am in the process of designing web-app, and would like to know what
I have been designing a site locally in PHP 5, but have come across
I'm designing a web site and I would like to be able to call
I am designing database for social networking site with followers and following feature like
I am in designing the site something like hardrock memorabilia using slverlight DeepZoom.. 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.