Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR conflict between other conditions

I have the following query:

    SELECT * FROM `Contacts` 
WHERE `Zona` = '1' 
AND `Responsable` = '9' 
AND `AllowComercialVisit` = 'Call_Again'
-- the problem are OR's --
OR `AllowComercialVisit` = 'Busy' 
OR `AllowComercialVisit` = 'Not_answered' 
-- the problem are OR's --
AND `DateRecall` <= '2016-06-20 13:04:52' 
AND `DateRecall` >= '2016-06-20 12:39:52' 
ORDER BY `DateRecall` ASC LIMIT 1

The problem is that the query should ONLY shows the rows between the first and the second 'DateRecall' , but return all the rows with 'Call_Again','Busy' and 'Not_answered' without filtering the date.

Any solution will be appreciated !

like image 216
Robert Blasco Villarroya Avatar asked Jun 20 '16 11:06

Robert Blasco Villarroya


People also ask

What is a conflict between two or more people called?

Interpersonal conflict refers to any type of conflict involving two or more people. It's different from an intrapersonal conflict, which refers to an internal conflict with yourself.

What are the conditions of conflict?

One group is of those conditions which worsen Conflict Behavior generally, whether negative communications, sanctions, violence, or war. These include the sociocultural dissimilarity between the parties, their cognitive imbalance and status difference and the coercive power of the parties.

What are the 4 types of conflict?

The opposing force created, the conflict within the story generally comes in four basic types: Conflict with the self, Conflict with others, Conflict with the environment and Conflict with the supernatural. Conflict with the self, the internal battle a lead character has within, is often the most powerful.

What are the 4 types of conflict in workplace?

According to Amy Gallo, who wrote the Harvard Business Review Guide to Managing Conflict at Work, there are four types of work conflict: status conflict, task conflict, process conflict, and relationship conflict.


1 Answers

A simple IN() would solve this:

SELECT * FROM `Contacts` 
WHERE `Zona` = '1' 
    AND `Responsable` = '9' 
    AND `AllowComercialVisit` IN ('Call_Again','Busy','Not_answered') 
    AND `DateRecall` BETWEEN '2016-06-20 12:39:52'
                         AND '2016-06-20 13:04:52' 
ORDER BY `DateRecall` ASC
LIMIT 1

In general, AND has precedence over OR , when using OR try using parentheses ->

WHERE COND1 AND COND2 AND (COND3 OR COND4) AND COND5

Which will force the optimizer to follow your precedence and not the default one.

like image 69
sagi Avatar answered Oct 29 '22 22:10

sagi