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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:12:55+00:00 2026-05-31T14:12:55+00:00

Not the best title, I apologise. Table structure: messages id – int message –

  • 0

Not the best title, I apologise.

Table structure:

messages

  • id – int
  • message – varchar
  • user – varchar
  • date – varchar
  • from – varchar

contacts

  • contactId – int
  • contactUser – varchar
  • contactName – varchar
  • contactFrom – varchar

My query currently is:

 SELECT
     messages1.`from`,
     messages1.`message`,
     messages1.`date` AS d,
     messages2.`count`,
     contacts.`contactId`,
     contacts.`contactName`,
     contacts.`contactFrom`
 FROM messages AS messages1,
   (
          SELECT
              MAX(`date`) AS maxdate,
             `from`,
             COUNT(*) AS `c`
          FROM messages
          WHERE
             `user` = 'MYUSER'
          GROUP BY `from`
   ) AS messages2
LEFT JOIN contacts ON
   (
       contacts.`contactUser` = 'MYUSER' AND
       contacts.`contactsFrom` = messages2.`from`
   )
WHERE
   messages1.`from`= messages2.`from` AND
   messages1.`date` = messages2.`date`
ORDER BY `date` DESC;

However, when a user has the same contact ‘from’ for two different users, they will get the same ‘conversation’ twice.

Eg:

If the following data exists:

contacts

| contactId | contactUser |  contactName | contactFrom |
|-----------|-------------|--------------|-------------|
|     1     |   giggsey   |  MyNameOne   | +1234567890 |
|     2     |   giggsey   | MySecondName | +1234567890 |

messages

|  id  |  message  |   user   |    date    |     from     |
|------|-----------|----------|------------|--------------|
|   1  |     h1    | giggsey  | 111111111  | +1234567890  |
|   2  |     h2    | giggsey  | 111111113  | +1234567890  |
|   3  |   random  | giggsey  | 111111116  | +9999992234  |
|   4  |     h3    | giggsey  | 111111119  | +1234567890  |

Then the query returns:

|     from    | message |      d    | count | contactId |  contactName | contactFrom |
|-------------|---------|-----------|-------|-----------|--------------|-------------|
| +1234567890 |    h3   | 111111119 |   3   |     1     |   MyNameOne  | +1234567890 |
| +1234567890 |    h3   | 111111119 |   3   |     2     | MySecondName | +1234567890 |
| +9999992234 |  random | 111111116 |   3   |  (NULL)   |     (NULL)   |   (NULL)    |

As you can see above, it is returning the same ‘conversation’ twice, but once for each duplicate of the contact.

I’m pretty sure it’s because of the JOIN, but I can’t seem to work out why.

  • 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-31T14:12:57+00:00Added an answer on May 31, 2026 at 2:12 pm

    Fixed it by using a subquery instead of just reading from the table.

     SELECT
         messages1.`from`,
         messages1.`message`,
         messages1.`date` AS d,
         messages2.`count`,
         contacts.`contactId`,
         contacts.`contactName`,
         contacts.`contactFrom`
     FROM messages AS messages1,
       (
              SELECT
                  MAX(`date`) AS maxdate,
                 `from`,
                 COUNT(*) AS `c`
              FROM messages
              WHERE
                 `user` = 'MYUSER'
              GROUP BY `from`
       ) AS messages2
    LEFT JOIN
      (
          SELECT
              *
          FROM
             `contacts`
          GROUP BY `contactsFrom`
      ) contacts ON
       (
           contacts.`contactUser` = 'MYUSER' AND
           contacts.`contactsFrom` = messages2.`from`
       )
    WHERE
       messages1.`from`= messages2.`from` AND
       messages1.`date` = messages2.`date`
    ORDER BY `date` DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for sure this is not the best title. I'm creating a system to generate
First of all, sorry about that title. I'm not the best at writing those
As you can probably tell by the title, I'm not sure the best way
Okay that's probably not the best title, I know why we need browser resets:
Alright, probably not the best title, but meh. I have the following code inside
Probably not the best title I've ever written, but I find it hard to
I admit it is not the best title, but I don't know how to
Not sure if that was the best Title in the world but I hope
Probably not the best title, but I'll explain: I have an array of objects
I'm not sure how best to describe this, or the best title, so bear

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.