views:

34

answers:

2

I have a script that automatically populates a mysql database with data every hour. It populates the date field like 03/17/10.12:34:11 and so on.

I'm working on pulling data based on 1 day at a time from a search script.

If i use

SELECT * 
  FROM call_logs 
 WHERE call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'

it works, but when I try to add the rest of the search params, it ignores the call_initiated field.

SELECT * 
  FROM call_logs 
 WHERE  caller_dn = '2x9xxx0000' OR called_dn = '2x9xxx0000' 
   AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'

^-- I x'd out a couple of the numbers. I've also tried without the between function, and used >= <= to pull the records, but have the same results. Im sure its an oversight, thanks in advance.

+3  A: 
  • try using parentheses around your OR statement ... where (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'

  • use the IN operator instead of OR ... where caller_dn IN('2x9xxx0000','2y9yyyy000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'

dweeves
ahh simple solution, the parans around the caller fields did the trick.. i was actually in the process of experimenting with those.. thanks!
Kelso
"i was actually in the process of experimenting with those." It is important to understand WHY this works, as with any programming issue, if you don't understand why, it will happen again.. see explanation below.
Rabbott
+2  A: 

The OR statement is more than likely the issue, since OR evaluates the left side, sees that its true, it doesn't care what is on the right site, because as long as one of the statements is true, it considiers the entire statement to be true.

Read up on the OR operator at the MYSQL page

Your Statement should look like

SELECT * FROM `call_logs` WHERE (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated BETWEEN '03/17/10.12:00:00' and '03/17/10.13:00:00';
Rabbott