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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:39:16+00:00 2026-05-28T05:39:16+00:00

today i need your help to get an specific sql select query. i have

  • 0

today i need your help to get an specific sql select query.

i have following table:

table with all results

and after a specific query regarding a specific id (in this case id 1) i wanna have a result like this:

user_id (an alias for the id_sender/id_recipient), date (maybe a max function, cause i wanna have the latest date to group), messages (a count function to the messages):

10 | 2012-01-14 09:10:05 | 4
11 | 2012-01-13 13:52:49 | 1
13 | 2012-01-13 20:01:17 | 1
14 | 2012-01-14 09:20:17 | 1

i tryed a lot but dont get the exact results – so my approach was something like this:

SELECT `id_recipient`, `id_sender`, MAX(`date`) AS `date`, COUNT(*) AS `messages` FROM `table` WHERE `id_recipient` = 1 OR `id_sender` = 1 GROUP BY `id_recipient`, `id_sender`

but then i get this result:

my approach

its not so bad but as u can see the 4th line should be included in the results of the first one.
i hope u got me. feel free to ask if smth is not clear.

thanks in advance,
greetings

  • 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-28T05:39:16+00:00Added an answer on May 28, 2026 at 5:39 am

    Ok, so since we know the value for id_recipient, we can use some math to trick SQL into getting this nasty query done.

    Let n be the id value of the person of interest.

    We know that the pairing of id_recipient and id_sender will ALWAYS include the user with id value n. Based on the where clause.

    Therefore, id_recipient + id_sender == n + id_otherPerson is true.

    The resulting query will be very similar to this.
    (It’s been a while, but I don’t think I have any syntax problems)

    SELECT (`id_recipient` + `id_sender` - n) AS `id_otherPerson`, 
            MAX(`date`) AS `date`, COUNT(*) AS `messages` 
    FROM `table` 
    WHERE `id_recipient` = n XOR `id_sender` = n 
    GROUP BY `id_otherPerson`;
    

    Edit: I’ve changed it to an XOR, so if person n messages person n, it won’t cause all values to be incremented by the number of times n has messaged themself.

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

Sidebar

Related Questions

I need your help again. This should be a function for php. I've got
I have today = new Date(); object. I need to get first and last
need your advices on this scenario: I have a bunch of services installed on
Im just starting with localization today and need some information. I have a project
How do I get today's date in C# in mm/dd/yyyy format? I need to
My Google-Fu is weak today, hopefully this is a simple thing. I need to
Could you help me with this question, please? I only need to put a
I have approached you today to try and gather your opinions and experience with
I got some help with this earlier today but I cannot figure out the
I need to set up two machines later today for a domo of a

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.