Construct a SQL query w.r.t optional query parameters

269 Views Asked by At

I am trying to construct a sql query with respect to optional query parameters from the endpoint.

e.g. I have a url http://localhost/api?one=1

Now form the above url I need to construct query somthing like ..

SELECT DISTINCT tb_one.id as id, tb_one.title as title from tb_one 
  JOIN
  tb_one ON tb_main.id = tb_one.id
  where other_id in (1) and tb_one.id in (2) 

and also e.g. i have a url http://localhost/api?one=1&two=1

SELECT DISTINCT tb_one.id as id, tb_one.title as title from tb_one 
      JOIN
      tb_one ON tb_main.id = tb_one.id 
      JOIN
      tb_two ON tb_main.id = tb_wo.id
      where other_id in (1) 

It could be n number of parameters with different combinations.

Can we construct a query like this in Go?

0

There are 0 best solutions below