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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:28:08+00:00 2026-05-27T02:28:08+00:00

I have a users like this: id username 1 user1 2 user2 3 user3

  • 0

I have a users like this:

id    username
1       user1
2       user2
3       user3

and a msgs like this:

t_id   sent_by   id    msg
1        2        1     whatever
2        3        1     is
3        2        1     here

Where users.id is a primary key and msgs.id is a foreign key. In the msgs table, id is the destination of the message sent by sent_by.

I want to select and display the username of sent_by as long as the logged in user (via sessions) is the msgs.id.

To clarify things, here is the pseudocode of what I wanted to do:

  1. Users has logged in. Store its userid to session.
  2. Display the distinct usernames of who sent me (the logged in user) the messages. In the example above, the display will be: user2, user3 if I have a user id of 1.

I was thinking of using join but ended up doing 2 queries for the sent_by and ids. It seems not an efficient query.

What should I do?

  • 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-27T02:28:09+00:00Added an answer on May 27, 2026 at 2:28 am

    This is a straightforward JOIN since you only wish to return usernames of the sent_by ids.

    $sql = "SELECT DISTINCT
        users.username
        msgs.sent_by
      FROM users JOIN msgs ON users.id = msgs.sent_by 
      WHERE id = {$_SESSION['my_userid']}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MySQL query like this: SELECT MAX(messages.ID) as maxID, messages.from, messages.read, users.userName
I have a USERS table like this USERS user_id | username 1 tom 2
I have a database with three tables set up like this: Users : username,
So I have a database schema like this: Users UserId RoleUserXRef RoleUserId RoleId UserId
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
I am using MySQL. I have several lookups like this: SELECT * from users
I have a table, in that table I have users something like this: id
Is there a way to query users table like this: | id | username
I have this problem. Given a users table that consists of users' username in
I have a database with two tables: Users , Items Users looks like this:

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.