Need help with a query: search by username instead of user_id

Please, help me to change the search. I need to search by username instead of user_id.

This is my query:

-- [params]
-- string :query = white_check_mark
-- integer :user
-- date :start_date
-- date :end_date

SELECT created_at, topic_id, p.id as post_id FROM posts p
LEFT JOIN post_search_data psd ON psd.post_id = p.id
WHERE psd.search_data @@ TO_TSQUERY(:query)
AND user_id = :user
AND p.created_at BETWEEN :start_date::date
AND :end_date::date

I hope it helps you.

-- [params]
-- string :query = white_check_mark
-- string :user
-- date :start_date
-- date :end_date

SELECT 
    p.created_at, 
    p.topic_id, 
    p.id as post_id 
FROM posts p
LEFT JOIN users u ON (p.user_id = u.id)
LEFT JOIN post_search_data psd ON psd.post_id = p.id
WHERE psd.search_data @@ TO_TSQUERY(:query)
      AND LOWER(username) LIKE '%'||LOWER(:user)||'%'
      AND p.created_at BETWEEN :start_date::date
      AND :end_date::date
6 Likes

Yes, that works. Thank you for your time!

1 Like