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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:14:11+00:00 2026-05-20T18:14:11+00:00

Background I have a table of comments. Each comment belongs to a specific row

  • 0

Background

I have a table of comments. Each comment belongs to a specific row in the ‘lead’ table or the ‘prod’ table. There can be multiple comments for each row in the ‘lead’ and ‘prod’ tables. There can also be multiple comments with the same ‘kstatus’ for each row.

comments
    INT id /* primary key */
    ENUM('lead', 'prod') type /* tells me whether the comment belongs to a row in the lead table or the prod table */
    INT fid /* foreign key, refers to the id column in either the lead or prod table */
    MEDIUMTEXT comment  /* the comment itself */
    INT timestamp /* when the comment was written, i.e. 1300530201 */
    INT kstatus /* foreign key, refers to the id column in the kstatus column */

lead
    INT id
    ...

prod
    INT id
    ...

kstatus
    INT id
    ...

A comment with type = ‘lead’ and fid = 105 refers to the row in lead with lead.id = 105.
A comment with type = ‘prod’ and fid = 105 refers to the row in prod with prod.id = 105.

Question

I want to SELECT the first comment given (if any, and based on timestamp) for each row in the ‘lead’ table with a certain kstatus (say 14).

Here is my attempt, which returns way too few results. Maybe due to mixing of fid’s referring to lead.id and fid’s referring to prod.id?

SELECT C1.*
FROM comments AS C1
LEFT JOIN comments AS C2 ON (C1.timestamp > C2.timestamp AND C1.fid = C2.fid)
WHERE C2.timestamp is NULL
AND C1.type = 'lead'
AND C1.kstatus = 14
GROUP BY C1.fid
  • 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-20T18:14:12+00:00Added an answer on May 20, 2026 at 6:14 pm
    select c2.*
    from (
        select min(c.id) realfirstcomment
        from (
            select fid, min(timestamp) as firstcomment
            from comments
            where type='lead' and kstatus=14
            group by fid
        ) f, comments c
        where f.fid = c.fid
          and c.type='lead' and c.kstatus=14
          and f.firstcomment = c.timestamp
        group by c.fid
    ) f2, c2
    where f2.realfirstcomment = c2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a dimension table that has a single record for each day.
I have a table which is zebra-striped (different background color for each odd rows).
I have a table (that can be fairly large....say 200x200 px) where each cell
I have a table where i want to change cell background on mouse over
I have given a table background image using css background-image property. The cells are
I have created a table in my application, I want to select (change background
I have a gradient image as a background for a table header(9x18pix). This image
I have the following code that produces a background image in a table cell.
I have a table, in the Table header (TH) I have a background with
Background: I have a table with 5 million address entries which I'd like to

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.