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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:43:26+00:00 2026-05-19T00:43:26+00:00

I have 2 tables: user: id, name message: sender_id, receiver_id, message, read_at, created_at There

  • 0

I have 2 tables:

user: id, name
message: sender_id, receiver_id, message, read_at, created_at

There are 2 results I need to retrieve and I’m trying to find the best solution. I have included queries that I’m using in the very end.

  1. I need to retrieve a list of users, and also with each user have information available whether there are any unread messages from each user (them as sender, me as receiver) and whether or not there are any read messages between us ( they send I’m receiver or I send they are receivers)

  2. I need Same as above, but only those members where there has been any messaging between us, sorted by unread first, then by last message received.

Can you advise please? Should this be done with joins or subqueries?

In first case I do not need the count, I just need to know whether or not there is at least one unread message. I’m posting code and my current queries, please have a look when you get a chance:

BTW, everything is the way I want in fist query.

My concern is: In second query I would like to order by messages.created_at, but I dont think I can do that with grouping? And also I dont know if this approach is the most optimized and fast.

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
)

INSERT INTO `user` VALUES (1,'User 1'),(2,'User 2'),(3,'User 3'),(4,'User 4'),(5,'User 5');

CREATE TABLE `message` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `sender_id` bigint(20) DEFAULT NULL,
  `receiver_id` bigint(20) DEFAULT NULL,
  `message` text,
  `read_at` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

INSERT INTO `message` VALUES (1,3,1,'Messge',NULL,'2010-10-10 10:10:10'),(2,1,4,'Hey','2010-10-10 10:10:12','2010-10-10 10:10:11'),(3,4,1,'Hello','2010-10-10 10:10:19','2010-10-10 10:10:15'),(4,1,4,'Again','2010-10-10 10:10:25','2010-10-10 10:10:21'),(5,3,1,'Hiii',NULL,'2010-10-10 10:10:21');

SELECT u.*, m_new.id as have_new, m.id as have_any
FROM user u 
LEFT JOIN message m_new ON (u.id = m_new.sender_id AND m_new.receiver_id = 1 AND m_new.read_at IS NULL)
LEFT JOIN message m ON ((u.id = m.sender_id AND m.receiver_id = 1) OR (u.id = m.receiver_id AND m.sender_id = 1))
GROUP BY u.id

SELECT u.*, m_new.id as have_new, m.id as have_any
FROM user u 
LEFT JOIN message m_new ON (u.id = m_new.sender_id AND m_new.receiver_id = 1 AND m_new.read_at IS NULL)
LEFT JOIN message m ON ((u.id = m.sender_id AND m.receiver_id = 1) OR (u.id = m.receiver_id AND m.sender_id = 1))
where m.id IS NOT NULL
GROUP BY u.id
  • 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-19T00:43:27+00:00Added an answer on May 19, 2026 at 12:43 am

    I’m fairly certain that JOINs are more performant than subqueries. One problem I see is that there aren’t any date or foreign key indexes in your table creation scripts. You’ll want those. Here’s how I’d accomplish query #2, if I understood your question correctly:

    SELECT u.*,
        m_new.id AS have_new,
        MAX(m_new.created_at) AS new_created,
        m.id AS have_any,
        MAX(m.created_at) AS created
    FROM USER u 
    LEFT JOIN message AS m_new
        ON u.id = m_new.sender_id
        AND m_new.receiver_id = 1
        AND m_new.read_at IS NULL
    LEFT JOIN message AS m
        ON (u.id = m.sender_id AND m.receiver_id = 1)
        OR (u.id = m.receiver_id AND m.sender_id = 1)
    WHERE m.id IS NOT NULL
    GROUP BY u.id
    ORDER BY new_created DESC,
        created DESC
    ;
    

    Here are a couple of good links in SO on JOINs vs subqueries:

    1. SQL: Join vs. subquery
    2. Subqueries vs joins
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, like these: Table People: VARCHAR Name INTEGER Age Table Message
I have these tables: CREATE TABLE User( Username varchar(15) NOT NULL, Name varchar(20) DEFAULT
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
i have a system there user(sender) can write a note to friends(receivers), number of
The goal is to retrieve all users (not messages) that have sent a message
I have the following tables: users: user_id user_name message: message_id thread_id to_id from_id title
I have 3 tables and need to select some recoreds, In 2 table of
I have two tables: Avatars: Id | UserId | Name | Size ----------------------------------------------- 1

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.