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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:46:43+00:00 2026-05-16T23:46:43+00:00

I have a string of 3 queries that are designed to Find which messages

  • 0

I have a string of 3 queries that are designed to

  1. Find which messages have other messages with the same id which represents replies
  2. Find which messages of the results from the first query have the specified user as entering the first message of that string of messages (min timestamp)
  3. Find the latest message of that string of messages (max timestamp)

The problem comes with the third query. I get the expected results up to the second query, then when the third is executed, without the MAX(timestamp) as max, I get the expected results. When I add that, I only get the first message for each string of messages when it should be the last, regardless of whether I use min or max and the row count says 1 row returned when there is 2 rows shown. Anyone got any ideas on where I went wrong?

$sql="SELECT reply_chunk_id 
        FROM messages 
        GROUP BY reply_chunk_id 
        HAVING count(reply_chunk_id) > 1 ";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array('specified_user'));
while($row = $stmt16->fetch(PDO::FETCH_ASSOC)){
    $sql="SELECT user,reply_chunk_id, MIN(timestamp) AS grp_timestamp
            FROM messages WHERE reply_chunk_id=?
            GROUP BY reply_chunk_id HAVING user=?";
    $stmt17 = $conn->prepare($sql);
    $result=$stmt17->execute(array($row['reply_chunk_id'],'specified_user'));
    while($row2 = $stmt17->fetch(PDO::FETCH_ASSOC)){
        $sql="SELECT message, MAX(timestamp) as max FROM messages WHERE reply_chunk_id=?";
        $stmt18 = $conn->prepare($sql);
        $result=$stmt18->execute(array($row2['reply_chunk_id']));
        while($row3 = $stmt18->fetch(PDO::FETCH_ASSOC)){
            echo '<p>'.$row3['message'];
        }
    }
}
echo ' '.$stmt18->rowCount();

Create table view of messages, as requested:

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `timestamp` int(11) NOT NULL,
  `user` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'anonimous',
  `message` varchar(2000) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `topic_id` varchar(35) NOT NULL,
  `reply_chunk_id` varchar(35) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
  • 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-16T23:46:44+00:00Added an answer on May 16, 2026 at 11:46 pm

    Since message isn’t grouped, exactly which message from the group you’ll get isn’t defined. If you want the message with the maximum timestamp, you’ll need to explicitly select it:

    SELECT message, timestamp AS max
        FROM messages 
        WHERE reply_chunk_id=:rcid
          AND timestamp=(SELECT MAX(timestamp) 
                             FROM messages 
                             WHERE reply_chunk_id=:rcid)
    

    or:

    SELECT message, timestamp AS max
        FROM messages 
        WHERE reply_chunk_id=?
        ORDER BY timestamp DESC, id
        LIMIT 1
    

    The second query breaks ties (in the very unlikely but possible situation that more than one person posts at the same time) by also selecting the message with the highest id.

    General feedback

    Many of the variables you set within the loops are invariant, and thus should be moved outside the loop.

    $stmt17 will return at most 1 result. Moreover, $stmt18 will return always return exactly one result. Rewriting the second inner while loop (for $stmt17) as an if statement, and simply fetching the result from $stmt18 would be equivalent and clearer as to purpose.

    try {
      $threadSql="SELECT reply_chunk_id 
              FROM messages 
              GROUP BY reply_chunk_id 
              HAVING count(reply_chunk_id) > 1 ";
      $firstUserSql="SELECT user, MIN(timestamp) AS grp_timestamp
              FROM messages WHERE reply_chunk_id=?
              GROUP BY reply_chunk_id HAVING user=?";
      $lastMsgSql="SELECT message, MAX(timestamp) as max FROM messages WHERE reply_chunk_id=?";
      $threadQuery = $conn->prepare($threadSql);
      $threadQuery->setFetchMode(PDO::FETCH_ASSOC);
      $firstUserQuery = $conn->prepare($firstUserSql);
      $lastMsgQuery = $conn->prepare($lastMsgSql);
    
      $result=$threadQuery->execute(array('specified_user'));
      foreach ($threadQuery AS $thread){
          $result=$firstUserQuery->execute(array($thread['reply_chunk_id'],'specified_user'));
          if (FALSE !== ($firstUser = $firstUserQuery->fetch(PDO::FETCH_ASSOC))) {
              $result=$lastMsgQuery->execute(array($thread['reply_chunk_id']));
              $lastMsg = $lastMsgQuery->fetch(PDO::FETCH_ASSOC);
              echo '<p>'.$lastMsg['message'].'</p>';
          }
      }
      echo ' ' . $lastMsgQuery->rowCount();
    } catch (PDOException $exc) {
      ...
    }
    

    Lastly, a single SQL statement can replace much of the PHP code:

    SELECT mchunk.reply_chunk_id, 
           muser.user, MIN(muser.`timestamp`) AS grp_timestamp, 
           mmax.message, mmax.`timestamp` AS max
      FROM messages AS mchunk
      JOIN messages AS muser
        ON mchunk.reply_chunk_id = muser.reply_chunk_id
      JOIN messages AS mmax
        ON mchunk.reply_chunk_id = mmax.reply_chunk_id
      WHERE mmax.timestamp=(SELECT MAX(timestamp) FROM messages AS m WHERE m.reply_chunk_id=mchunk.reply_chunk_id)
      GROUP BY mchunk.reply_chunk_id, muser.user
      HAVING count(mchunk.reply_chunk_id) > 1
        AND muser.user IN ('steve', '0010')
    ;
    

    This selects all threads started by a specified user that have responses, along with the most recent response.

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

Sidebar

Related Questions

I have string that look like Array that fetched from other webservice like this
I've 16 queries that I need to execute. All these queries have the same
I have multiple linq queries that retrieve the same data just at different grouping
I have been messing with Oracle DB queries that run from my JAVA app.
I have a set of sql - queries: List<String> queries = ... queries[0] =
I have a method which takes an array of strings as parameter and queries
I have string that displays UTF-8 encoded characters, and I want to convert it
I'm working on a software that does extensive queries to a database which is
I have some code that queries Active Directory to verify user existence. I am
Basically, I have a string of functions that load a set of blog entries

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.