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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:13:03+00:00 2026-05-27T18:13:03+00:00

I have three tables: products (product_id, title) comments (comment_id, product_id, user_id, comment, post_date) bookmarks

  • 0

I have three tables:

  • products (product_id, title)
  • comments (comment_id, product_id, user_id, comment, post_date)
  • bookmarks (user_id, product_id, read_date)

For each product_id in the products table, I wish to retrieve the number of comments with the same product_id, and whose post_date value is greater than the read_date value for the row in the bookmarks table that shares this product_id, and has user_id=22.

If such a row does not exist in the bookmarks table, I want to retrieve the total number of comments for that product_id regardless of read_date.

So far I have

SELECT p.product_id, COUNT( c.comment_id ) comment_count
FROM products p
LEFT JOIN bookmarks b, comments c ON b.product_id = c.product_id
AND b.user_id =22
AND (
c.post_date > b.read_date
)
AND p.product_id = c.product_id
GROUP BY c.product_id
ORDER BY comment_count DESC

This does not give me the expected results. How can I modify it to make it do what I want?

  • 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-27T18:13:04+00:00Added an answer on May 27, 2026 at 6:13 pm

    Will it work for you ?

    SELECT p.product_id, 
    COUNT(CASE 
      WHEN b.read_date IS NOT NULL AND c.post_date >b.read_date THEN c.comment_id
      WHEN b.read_date IS NULL THEN c.comment_id
      ELSE NULL //optional, CASE has default ELSE NULL
    END) as comment_count
    FROM products p
    LEFT JOIN bookmarks b ON (b.product_id = p.product_id AND b.user_id=22) 
    LEFT JOIN comments c ON (p.product_id = c.product_id)
    GROUP BY p.product_id
    ORDER BY comment_count DESC
    

    UPDATE
    GROUP BY c.product_id changed to GROUP BY p.product_id

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

Sidebar

Related Questions

I have this three tables: products TABLE: id:integer name:string features TABLE: id:integer name:string features_products
I have the following three tables in a database: Products ******** id title artist_id
I have 3 tables (mysql) products product_id, title categories category_id, parent_id, title products_categories product_id,
I have three tables: 'users', 'products', and 'product_types'. The 'users' table stores all usual
Suppose i have tables Products -------product_id , name , price , size shopping_cart------cart_id,item_id,user_id,quantity order----order_id
I have 3 tables Products product_id max_products_can_sell max_products_can_sell_to_individual purchases user_id product_id quantity reservations product_id
I have 3 tables: events, products and a association table event_products. events:event_id, products: product_id,
I have the following three database tables: Products ######## id title artist_id Arists ######
I have three tables like this: Person table: person_id | name | dob --------------------------------
I have three tables. Product, product categories and product category links. The links table

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.