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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:41:39+00:00 2026-06-11T22:41:39+00:00

I have a personal message system on my website, which is pretty easy. But

  • 0

I have a personal message system on my website, which is pretty easy.
But I would like to have an administrator page where all conversations are shown between users and their amount of messages.

So the table looks like (simplified version):

CREATE TABLE pm (
    id        INT(10)     NOT NULL AUTO_INCREMENT,
    from      INT(10)     NOT NULL REFERENCES (usertable),
    to        INT(10)     NOT NULL REFERENCES (usertable),
    message   BLOB        NOT NULL
);

Example:

Say I have some users: Mark, John, Bryan and Kate.

Mark (from) sends 5 messages to John (to) and John (from) sends 3 messages to Mark (to).

Kate (from) sends 2 messages to Bryan (to) and Bryan (from) sends 1 message to Kate (to).

I would like a result set that shows

Mark – John – 8 messages

Kate – Bryan – 3 messages

And this for all users in my table at once.

I’m really stuck on this and I have searched everywhere but have not found a solution.
The difficulty lies in the fact that I want all users listed and I have to cross the "from" and "to" column somehow…

I hope anyone will be able to help.
Thanks in advance.

  • 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-11T22:41:40+00:00Added an answer on June 11, 2026 at 10:41 pm
    select from_id, to_id, count(*) count_between
    from
    (
        select from_id, to_id from pm
        union all
        select to_id, from_id from pm
    ) combined
    where from_id < to_id
    group by from_id, to_id
    

    Full sample

    CREATE TABLE pm (from_id int,to_id int);
    insert pm select 1,2;
    insert pm select 1,2;
    insert pm select 1,2;
    insert pm select 1,2;
    insert pm select 1,2;
    insert pm select 2,1;
    insert pm select 2,1;
    insert pm select 2,1;
    insert pm select 3,4;
    insert pm select 3,4;
    insert pm select 4,3;
    
    select from_id, to_id, count(*) count_between
    from
    (
        select from_id, to_id from pm
        union all
        select to_id, from_id from pm
    ) combined
    where from_id < to_id
    group by from_id, to_id
    
    --- results
    from_id     to_id       count_between
    ----------- ----------- -------------
    1           2           8
    3           4           3
    

    To turn the IDs into names, use the normal to user table or some such. e.g.

    select u1.name from_, u2.name to_, count(*) count_between
    from
    (
        select from_id, to_id from pm
        union all
        select to_id, from_id from pm
    ) combined
    join users u1 on u1.id = combined.from_id
    join users u2 on u2.id = combined.to_id
    where from_id < to_id
    group by u1.name, u2.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a personal message system in my website done simply with php/sql. Actually
Merry Christmas everybody!!! I have a personal message system in my page. To show
I am trying to create a personal inbox message system but couple question are
I have a small application, which is free for personal use, but requires a
I have a website with Facebook capabilities (Sending personal messages, posting to wall). I
I have my personal website at www.yassershaikh.com, now I am planning to setup a
I have a message class that I use like so: RedirectMsg::go('somepage.php', MessageType::ERROR, 'Your message
I have to send emails when a person receives a personal message on my
I have this iPhone like checkbox script, which makes beautiful checkboxes. The code is
I have a personal project which is stored in an svn repository in my

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.