2011年1月15日星期六

[fw-db] How can I group my queries with a Zend_Db_Select statement?

Hi,

Suppose I want to do a query like this:

SELECT * FROM Scorecards WHERE
(firstname LIKE 'Paul' OR lastname LIKE 'Paul')
AND gamedate = '2010-06-25'

What we want to do is find all "scorecards" for athletes with a first OR
last name that is paul, and where the event occurred on June 25th, 2010.

What I can't figure out is how to do the parenthetical grouping--I know I
can do something like this:

$select = $select->where('firstname LIKE ?', 'Paul');
$select = $select->orWhere('lastname LIKE ?', 'Paul');
$select = $select->where('gamedate ?', '2010-06-25');

But this produces a query without any grouping of the conditionals, like
this:

SELECT * FROM Scorecards WHERE
firstname LIKE 'Paul'
OR lastname LIKE 'Paul'
AND gamedate = '2010-06-25'

At least in the case of MySQL, this winds up returning all scorecards where
the first name is Paul (regardless of date), and all scorecards where the
lastname is Paul and the gamedate is June 25th, 2010. In effect, the
grouping becomes like this:

SELECT * FROM Scorecards WHERE
firstname LIKE 'Paul
OR (lastname LIKE 'Paul' AND gamedate = '2010-06-25')

So... what do I do?
--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/How-can-I-group-my-queries-with-a-Zend-Db-Select-statement-tp3219254p3219254.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: