youroff Reply #1, 4 months, 1 week ago
Hi there!
I'm trying to build query to count items that fit complex conditions, like this:
I tried to count items using simple queries, like following (and it works fine):
But when I try to put that condition in where clause as a string it doesn't work at all showing me all rows counted:
How do I set condition as a string and bind values?
I'm trying to build query to count items that fit complex conditions, like this:
SELECT COUNT(*) FROM `modx_schedule_items`
WHERE '08:00' BETWEEN `start_time` AND `end_time` OR
'08:30' BETWEEN `start_time` AND `end_time` OR
('08:00' <= `start_time` AND '08:30' >= `end_time`)
I tried to count items using simple queries, like following (and it works fine):
$q = $this->modx->newQuery($this->classKey);
$q->where(array('trainer_id' => $this->getProperty('trainer_id')));
$xcount = $this->modx->getCount('ScheduleItem', $q);
But when I try to put that condition in where clause as a string it doesn't work at all showing me all rows counted:
$q = $this->modx->newQuery($this->classKey);
$q->where(':start_time BETWEEN `start_time` AND `end_time` OR
:end_time BETWEEN `start_time` AND `end_time` OR
(:start_time <= `start_time` AND :end_time >= `end_time`)', array(
':start_time' => $some_starttime,
':end_time' => $some_endtime
));
$xcount = $this->modx->getCount('ScheduleItem', $q);
How do I set condition as a string and bind values?
perator => binding_value" format using PDO parameter bindings. When you need to use complex expressions like this, fall back to a string literal SQL expression.