remax-backend-rs/sql/property/fetch_paged_with_filters.sql

24 lines
1.3 KiB
SQL

SELECT
p.id,
p.title,
p.description,
p.agent_id,
p.state as "state: _",
p.time_created,
p.last_updated
FROM property p, property_details pd, location l where pd.property_id = p.id AND pd.location_id = l.id
AND (p.time_created <= $1 OR $1 IS null) -- Before Filter
AND (p.time_created >= $2 OR $2 IS null) -- After Filter
AND (LOWER(p.title) LIKE '%' || LOWER($3) || '%' OR $3 IS null) -- Title filter (like)
AND (LOWER(p.description) LIKE '%' || LOWER($4) || '%' OR $4 IS null) -- Description Filter (like)
AND (pd.meters > $5 OR $5 IS null) -- Bigger than or equal to filter
AND (pd.meters < $6 OR $6 IS null) -- Smaller than or equal to filter
AND (LOWER(l.country) LIKE '%' || LOWER($7) || '%' OR $7 IS null) -- Location Filter
AND (LOWER(l.province) LIKE '%' || LOWER($8) || '%' OR $8 IS null) -- Location Filter
AND (LOWER(l.city) LIKE '%' || LOWER($9) || '%' OR $9 IS null) -- Location Filter
AND (LOWER(l.district) LIKE '%' || LOWER($10) || '%' OR $10 IS null) -- Location Filter
AND (pd.listing_type LIKE $11 || '%' OR $11 IS null) -- Listing type filter
AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 >= $12::FLOAT8 OR $12 IS null) -- More expensive than filter
AND (split_part(pd.listing_type, ' ', 2)::FLOAT8 <= $13::FLOAT8 OR $13 IS null) -- More expensive than filter
ORDER BY p.time_created DESC
LIMIT 25 OFFSET $14