Hey all! I’m trying to build some search functiona...
# javascript
l
Hey all! I’m trying to build some search functionality that can resolve data from multiple tables depending on the context. Would it make more sense for me to write the table switching logic outside of the js api or is there a way I can do text search on multiple tables at once? For example, a user puts a street address into search which should return a real estate property. The same search box can be used to retrieve user profiles as well. I know I can query multiple tables as separate requests, but I’d love to do it in a single request. I’m really new to SQL and Postgres so forgive me if the answer is obvious!
k
Definitely the way here is to make one single request. Something like
Copy code
supabase.from(properties)
.select(*, creator:creator_id(id, name, picture))
.ilike(address, '%$addtext%'),
Having in mind that ur properties table has a creator id and that Creator id is a foreign key on ur profile/user table
b
If this becomes a complex query, you should write a PostgreSQL function for it and call it with
.rpc()
. For
SQL
you want to look into the
UNION
clause, which lets you merge results from querying multiple tables.
l
Thanks @burggraf and @Kosh!
@burggraf the union clause was exactly what I was looking for! Perfect example of SQL newbie lack of full understanding!
b
Great! Keep learning about SQL -- it's so much fun!
l
@burggraf I will! I find it fascinating. Coming from the front end I’ve had to solve a lot of these problems myself with data from an api, without being able to do the computations where they belong!