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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:05:16+00:00 2026-06-08T11:05:16+00:00

I have 3 Tables: Messages, Users, and Friends. Messages: msg_id, uid_fk, message, alert, created,

  • 0

I have 3 Tables: Messages, Users, and Friends.

Messages: msg_id, uid_fk, message, alert, created, uploads, owner
Users: uid, first_name, last_name
Friends: friend_one, friend_one

When a user becomes friends with another user, I record their uid’s in the following manner: friend_one:172 friend_two:58 | friend_one: 58 friend_two: 172
My code below works perfectly and goes through to select all of the user’s message updates. My problem is that I need to select all of the messages of not only the user himself/herself but also all of his or her friends messages too.

The following below as mentioned above selects only the user’s status updates and I have been struggling for hours to select not only the user’s status updates but also his or her friends with it.

$query = $db->prepare("SELECT M.msg_id, M.uid_fk, M.message, M.alert, 
M.created, M.uploads , M.owner , U.uid , U.first_name , U.last_name FROM 
messages M, users U  WHERE M.uid_fk=U.uid and M.uid_fk= :uid_fk limit 10");
$query->execute(array(':uid_fk' => $uid));

($uid is session id of logged-in user)

Also my code for selecting friends just in case:

$friends_sql = $db->prepare("SELECT a.first_name, a.uid, a.last_name FROM users 
a, friends b WHERE a.uid = b.friend_two AND b.friend_one = :friend_one ORDER BY 
b.friend_id DESC LIMIT 8");
$friends_sql->execute(array(':friend_one' => $uid));
  • 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-08T11:05:18+00:00Added an answer on June 8, 2026 at 11:05 am

    The following should return the most recent 10 messages for a user and the user’s friends.

    SELECT 
        M.msg_id, 
        M.uid_fk, 
        M.message, 
        M.alert, 
        M.created, 
        M.uploads, 
        M.owner, 
        U.uid, 
        U.first_name, 
        U.last_name 
    FROM 
        users u 
        JOIN messages m ON m.uid_fk = u.uid
    WHERE
        u.uid = :uid
        OR u.uid IN (
            SELECT f.friend_two
            FROM friends f
            WHERE f.friend_one = :uid
        )
    ORDER BY m.created DESC
    LIMIT 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to tables:users and messages.And create the one-many relations between these tables.And,If I
I have some tables like this: USERS TABLE: | id | created | active
I have the following tables: users: user_id user_name message: message_id thread_id to_id from_id title
I have 3 tables; Inbox, Outbox, and Messages. For a single message, the data
I have 3 tables, each table will show records of users posting messages. I
I have 2 tables: Users: Id, UserName, ... Messages: Id, MsgText, IsRead,.... I want
Say I have the following tables: users ----- id username messages ----- id sender
Consider the following tables: users messages ------------------- ---------------------- user_id avg_quality msg_id user_id quality -------------------
I have a simple table maintaining messages between users. The table structure looks like
I have Messages and User models with corresponding tables. The Messages table has such

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.