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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:36:39+00:00 2026-05-23T21:36:39+00:00

SELECT `posts`.`id`, `posts`.`created_at`, `posts`.`content`, `posts`.`replies`, `groups`.`id` AS `group_id`, `groups`.`name` AS `group_name`, `users`.`id` AS `user_id`,

  • 0
SELECT
    `posts`.`id`,
    `posts`.`created_at`,
    `posts`.`content`,
    `posts`.`replies`,
    `groups`.`id` AS `group_id`,
    `groups`.`name` AS `group_name`,
    `users`.`id` AS `user_id`,
    `users`.`name`,
    `users`.`surname`,
    `users`.`avatar`,
    `posts`.`link`,
    `posts`.`event_id`,
    `events`.`name` AS `event_name`,
    `docs`.`id` AS `attachment_id`
FROM `posts`
JOIN `groups`
    ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users`
    ON (`users`.`id` = `posts`.`user_id`)
LEFT JOIN `events`
    ON (`events`.`id` = `posts`.`event_id`)
LEFT JOIN `docs`
    ON (`docs`.`post_id` = `posts`.`id`)
WHERE `posts`.`post_id` = 0
    AND `posts`.`group_id` = '28'
ORDER BY `posts`.`id` DESC
LIMIT 4
OFFSET 0

This query selects posts for current group. Last thing I need to add is that it selects documents (docs) that are related to that post…

...
`docs`.`id` AS `attachment_id`
...
LEFT JOIN `docs`
    ON (`docs`.`post_id` = `posts`.`id`)
...

I will need hashed_name and name from docs table. At the moment, it selects only id, but its not the biggest problem. Problem is that one post may have more that one docs related to it.

In that case, in docs table id (primary keys) aren’t the same, but post_id (so its related to that post) are. For now, if post have more than one attachment, I get ‘n’ entries back… so I have like duplicate posts. That’s the problem.

The goal is to, afterwards, loop trough array like this:

<?php

    if(!empty($posts)) {

        foreach ($posts as $post) {
            echo $post['content'];

            foreach ($post['docs'] as $doc) {
                echo $doc['name'];
                echo $doc['hashed_name'];
            }
        }

    }

?>

How to modify query to get that result? Thanks in advice!

  • 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-23T21:36:40+00:00Added an answer on May 23, 2026 at 9:36 pm

    You need to reorganize the array. You are always going to get back a flat result so youll actually have every doc+post+event… you need need to loop through the result an then organize them into a nested structure by whatever you want the primary record to be (in this case the post).

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

Sidebar

Related Questions

i have users table and i have posts table i want select from users
I need to do a query like this: SELECT wposts.ID FROM posts wposts WHERE
Problem Given the following two tables, I'd like to select all Ids for Posts
if (isset($_POST['login'])) { $query = mysql_query(" SELECT id FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."'
What does the @posts line do exactly? def index if params[:user_id] && params[:artist_id] @id
The sql statement is like this: select posts.id, posts.title from posts inner join (select
I have two items, users and posts, and a join table linking them together.
SELECT DISTINCT wposts.* FROM wp_2_posts wposts, wp_2_postmeta wpostmeta, wp_2_postmeta wpostmeta1, wp_2_term_taxonomy, wp_2_terms, wp_2_term_relationships WHERE
I have two tables - forum_topics and topics_posts . I want to select rows
say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');

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.