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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:37:06+00:00 2026-05-25T10:37:06+00:00

i am having trouble creating a single mysql query for what i am trying

  • 0

i am having trouble creating a single mysql query for what i am trying to do here.

first off, i will show you the table structures and fields of the tables i am using for this particular query:

users:
- id
- name
- photo_name
- photo_ext

user_attacks:
- id
- level

user_news_feed:
- id
- id_user
- id_status
- id_attack
- id_profile
- id_wall
- the_date

user_status:
- id
- status

user_wall:
- id
- id_user
- id_poster
- post

whenever the user posts an attack, or status update, updates their profile, or posts on someones wall, it inserts the relevant data into its respective table and also inserts a new row into the user_news_feed table.

now, what i want to do is select the last 10 news feed items from the database. these news feed items need to grab relevant data from other tables as long as their value is not 0. so if the news feed is for a status update, the id_status would be the id of the status update in the user_status table, and the “status” would be the data needing to be selected via a left join. hope that makes sense.

heres my first mysql query:

$sql = mysql_query("select n.id_user, n.id_status, n.id_attack, n.id_profile, n.id_wall, n.the_date, u.id, u.name, u.photo_name, u.photo_ext, s.status
                    from `user_news_feed` as n
                    left join `users` u on (u.id = n.id_user)
                    left join `user_status` s on (s.id = n.id_status)
                    where n.id_user='".$_GET['id']."'
                    order by n.id desc
                    limit 10
                    ");

now this works great, except for 1 problem. as you can see the user_wall table contains the id’s for 2 different users. id_user is the user id the post is being made for, and id_poster is the user id of the person making that wall post. if the user makes a wall post on his/her own wall, it is inserted into the database as a status update into the user_status table instead.

so i have a conditional statement within the while loop for the first query, which has another sql query within it. here is the whole code for the while loop and second sql query:

while ($row = mysql_fetch_assoc($sql))
{
    if ($row['id_wall'] != 0)
    {
        $sql_u = mysql_query("select u.id, u.name, u.photo_name, u.photo_ext, w.post
                              from `user_wall` as w
                              left join `users` u on (u.id = w.id_poster)
                              where w.id='".$row['id_wall']."'
                              ");

        while ($row_u = mysql_fetch_assoc($sql_u))
        {
            $row['photo_name'] = $row_u['photo_name'];
            $row['photo_ext'] = $row_u['photo_ext'];
            $row['id_user'] = $row_u['id'];
            $row['name'] = $row_u['name'];
            $content = $row_u['post'];
        }
    }
    else
    {
        if ($row['id_status'] != 0)
            $content = $row['status'];
        else if ($row['id_attack'] != 0)
            $content = '<i>Had an attack</i>';
        else if ($row['id_profile'] != 0)
            $content = '<i>Updated profile</i>';
    }

    echo '<li'.(($count == $total_count) ? ' class="last"' : '').'>';

    echo '<img src="images/profile/'.$row['photo_name'].'_thumb.'.$row['photo_ext'].'" alt="" />';

    echo '<div class="content">';
    echo '<b><a href="profile.php?id='.$row['id_user'].'">'.$row['name'].'</a></b>';
    echo '<span>'.$content.'</span>';
    echo '<small>'.date('F j, Y \a\t g:ia', $row['the_date']).'</small>';
    echo '</div>';

    echo '<div style="clear: both;"></div>';

    echo '</li>';
}

i hope what i am trying to do here makes sense. so basically i want to have both sql queries ($sql, and $sql_u) combined into a single query so i do not have to query the database every single time when the user_news_feed item is a wall post.

any help would be greatly appreciated and i apologise if this is confusing.

  • 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-25T10:37:07+00:00Added an answer on May 25, 2026 at 10:37 am
    SELECT n.id_user, n.id_status, n.id_attack, n.id_profile, n.id_wall, n.the_date,
           u.id, u.name, u.photo_name, u.photo_ext, s.status,
           w.id AS wall_user_id, w.name AS wall_user_name,
           w.photo_name AS wall_user_photo_name,
           w.photo_ext  AS wall_user_photo_ext,
           w.post
      FROM user_news_feed AS n
      LEFT JOIN users AS u ON (u.id = n.id_user)
      LEFT JOIN user_status s ON (s.id = n.id_status)
      LEFT JOIN (SELECT a.id AS id_wall, b.id, b.name, b.photo_name, b.photo_ext, a.post
                   FROM user_wall  AS a
                   LEFT JOIN users AS b ON (b.id = a.id_poster)
                ) AS w ON w.id_wall = n.id_wall
     WHERE n.id_user = ?
     ORDER BY n.id desc
     LIMIT 10
    

    The ‘?’ is a placeholder where you can provide the value of $_GET['id'].

    Basically, this adds an extra outer join, to the main query (and some extra columns, which will be NULL if the news feed event is not a wall posting), but the outer join is itself the result of an outer join.

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

Sidebar

Related Questions

I have a SQL query I'm having trouble creating using NHibernate Criteria: SELECT ID,
I'm having trouble creating a custom close button for my overlay. Here's what the
I'm having trouble creating a table and putting data pulled from SQL into it.
I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
I'm having trouble creating multiple pages in a PrintDocument and displaying them within a
I'm having trouble creating a port in Unix. This code keeps returning Error creating
I'm having trouble creating a global array that i can use in other functions.
SOLVED. See below for the corrections (labeled FIXED). I'm having trouble creating a shared

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.