How do I modify this query to only return rows that have at least one row in a different table

troubleshooting

I have a query that works great, but now I want to only show users that have private photo albums. The secondary table is called `posts` and also has a `user_id` column, but the other column is called `private` and should be '1' if the album is private.

I'm not very good with JOINS, etc. and keep getting server errors when I try various things.

Current query:

SELECT DISTINCT `user_id`,
 ( 6371 * Acos(Cos(Radians('44.03986620')) * Cos(Radians(lat)) *
  Cos(Radians(lng) - Radians('-69.41447060')) + Sin(Radians('44.03986620')) *S in(Radians(lat))) ) AS distance
FROM   users
WHERE  `user_id` <> '1'      
       AND `lat` <> '0'
       AND `lng` <> '0'
HAVING ( distance > 0
          OR distance IS NOT NULL )
ORDER  BY distance ASC
LIMIT  0, 200 

It failed when I tried inserting this between the WHERE clause and `user_id` <> '1':

user_id IN
(
       SELECT user_id
       FROM   posts
       WHERE  users.user_id = posts.user_id
       AND    posts.private = '1'
)

Please help if anyone knows how to make this work.

Posted Jan 11, 2026, 8:06 PM UTC

12
Loading comments…