Error filtering SQL query

0

It is assumed that this query has to filter for all the conditions that are after the where or combine the conditions but, when I put a date range, a name and an interaction title, the data does not they filter as they should.

SELECT DISTINCT cl.first_name as Nombre, cl.last_name as apellido,cl.id_personal as Cedula, cl.phone as phone,it.title as interacion, fecha
FROM (SELECT interactions.create_date as fecha FROM interactions WHERE interactions.create_date BETWEEN '2017-10-23' AND '2017-10-23') sa, clients_profile cl left join interactions it ON cl.id = it.id_client_profile
WHERE   cl.first_name = '' OR
        cl.last_name =  '' OR
        cl.phone = '' OR
        cl.id_personal ='' OR
        it.title = '';

This is an example of the result that I get when trying to filter by the 2 fields, what you happen is that I seek to obtain a query where you filter the data through the parameters and if you do not send one that filters by at least 1, in this case would be the dates

    
asked by Julio Vasquez 25.10.2017 в 03:06
source

1 answer

1

Your query (simplified) is

SELECT DISTINCT 
   cl.first_name as Nombre, 
   cl.last_name as apellido,
   cl.id_personal as Cedula, 
   cl.phone as phone,
   it.title as interacion, 
   fecha
FROM clients_profile cl 
left join interactions it ON cl.id = it.id_client_profile
WHERE   cl.first_name = 'delvin' OR
        it.create_date BETWEEN '2017-10-23' AND '2017-10-23'

You are asking for all records where the name is 'delvin' OR the date is between the two dates you entered. The result will give you all the interactions of 'delvin' throughout eternity, and all the interactions of the rest of the people in the date range.

If you send the two parameters, then the condition should be a AND instead of a OR

    
answered by 25.10.2017 в 15:25