0

I'm implementing a search function with multiple fields and using @Query. But when I send all params, I want skip from Like fields that I don't send from frontend. For example:

select dp.* from cat_productos p 
inner join cat_datos_producto dp on dp.prd_id = p.prd_id
inner join cat_categorias c on dp.cat_id = c.cat_id 
inner join cat_empresas e on dp.emp_id = e.emp_id
where  p.prd_sku LIKE '%'  or 
UPPER(p.prd_descripcion) like '%%' and UPPER(p.prd_departamento) like '%%' and 
UPPER(p.prd_marca) like '%%' and p.prd_codigo_int LIKE '%'  and 
c.cat_id = 2 and e.emp_id = 1;

This query return always all data because only c.cat_id and e.emp_id have values and the others have an empty string.

But if, from frontend I just send cat_id and emp_id, I want to skip the fields for example prd_sku, and prd_descripcion. Also the other fields that have empty string like this:

select dp.* from cat_productos p 
inner join cat_datos_producto dp on dp.prd_id = p.prd_id
inner join cat_categorias c on dp.cat_id = c.cat_id 
inner join cat_empresas e on dp.emp_id = e.emp_id
where c.cat_id = 2 and e.emp_id = 1;
Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
  • This question is unclear. Please provide some context like which programming lang. do you use,do you generate SQL via code or in the database, if database is it a view or a procedure? – Nava Bogatee May 15 '20 at 00:21
  • Does this answer your question? [Dynamic spring data jpa repository query with arbitrary AND clauses](https://stackoverflow.com/questions/28874135/dynamic-spring-data-jpa-repository-query-with-arbitrary-and-clauses) – Jens Schauder May 15 '20 at 10:20

0 Answers0