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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:06:29+00:00 2026-05-28T17:06:29+00:00

My database have got 4 table : users id username images id user_id image

  • 0

My database have got 4 table :

users

  • id
  • username

images

  • id
  • user_id
  • image

user_follow

  • user_id
  • user_follow

commentaries

  • image_id
  • text

I try to make a query to get all my and my friends pictures
1) row must be shown only if user have picture in “images” table
2) only image from me and my friends (depending on “user_follow” table)
3) count commentaries for each picture

my query is:

SELECT u.username as user, i.image as user_image, p.image, UNIX_TIMESTAMP(p.date) as date, COALESCE ( imgcount.cnt, 0 ) as comments
FROM users u              
LEFT JOIN user_follow f ON u.id = f.follow_id
LEFT JOIN images p ON p.user_id = u.id
LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
LEFT JOIN (SELECT image_id, COUNT(*) as cnt FROM commentaries GROUP BY image_id  ) imgcount ON p.id = imgcount.image_id
WHERE f.user_id = 3 OR p.user_id = 3       
ORDER BY p.date DESC

Commentaries count of each image sql line works fine in this query

LEFT JOIN (SELECT image_id, COUNT(*) as cnt FROM commentaries GROUP BY image_id  ) imgcount ON p.id = imgcount.image_id

in this line I try to get user his last upload image as avatar from “images” table

   LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)

This query dot not return result correctly because it show my friends who do not have pictures and something is not good with my own pictures (in database I have 2 row with my user_id : 3 ) but sql return 4

  • 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-28T17:06:30+00:00Added an answer on May 28, 2026 at 5:06 pm

    If I’m understanding correctly, it seems like you’ve made this a lot more complex than it needs to be. The reason you are getting records for your friends without pictures is because you are using a LEFT JOIN. Change that to an INNER JOIN and you will only get the records that match the join condition.

    I think you are looking for something like the following. You’ll need to make a few tweaks but hopefully this helps.

    SELECT u.username as user, i.image as user_image, count(*) as comments
    FROM users u              
    INNER JOIN user_follow f ON u.id = f.follow_id
    INNER JOIN images p ON p.user_id = u.id
    INNER JOIN commentaries c ON c.image_id = p.id
    WHERE u.user_id = 3
    GROUP BY u.username, i.image;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a Database like this: users -id user_cars -user_id -car_id cars -id
I am using external Users database for different projects. Now I have got School
I've got an Oracle 10g database, and I have a third-party jar file. I
I've got an MS-Access database stored in a folder (yes, I have 'Modify' permissions
If you've got a database setup in Django, how can you have the TestRunner
I have a FormView control in an ASP.NET 2.0 app. I've got the database
I have database with many tables. In the first table, I have a field
I got a table with columns: author firstname, author lastname, and booktitle Multiple users
Ive got a table of thousands of products and 50 or so authenticated users.
I have a simple database with two tables. Users and Configurations. A user has

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.