I know Zend provides a having() method, but what I want is a query like:
SELECT a.*, `as`.* FROM `fruit_db`.`apples` AS `a`
INNER JOIN `fruit_db`.`apple_seeds` AS `as` ON a.id = as.apple_id
WHERE (a.id = 1) AND as.seed_name HAVING 'johnny'
not "HAVING (as.seed_name = 'johnny')"
Backtracking a bit, we have the tables:
fruit_db.apples
| id | name |
--------------
| 1 | red |
| 2 | green|
fruit_db.apple_seeds
| apple_id | seed_name |
------------------------
| 1 | johnny |
| 1 | judy |
| 2 | granny |
I want the results like:
| id | name | apple_id | seed_name |
-------------------------------------
| 1 | red | 1 | johnny |
| 1 | red | 1 | judy |
The above query provided gives this result, but using Zend_Db_Select puts parenthesis around each portion of the having and where statements which invalidates my query. So
$zend_db_table->select()
->setIntegrityCheck(false)
->from(array("a" => "apples"), array("*"))
->join(array("as"=>"apple_seeds"),
"a.id = as.apple_id",
array("*"))
->where('a.id = 1')
->where('as.seed_name HAVING "johnny"');
produces:
SELECT a.*, `as`.* FROM `fruit_db`.`apples` AS `a`
INNER JOIN `fruit_db`.`apple_seeds` AS `as` ON a.id = as.apple_id
WHERE (a.id = 1) AND (as.seed_name HAVING 'johnny')
Which is invalid SQL. In short:
SELECT a.*, `as`.* FROM `fruit_db`.`apples` AS `a`
INNER JOIN `fruit_db`.`apple_seeds` AS `as` ON a.id = as.apple_id
WHERE (a.id = 1) AND as.seed_name HAVING 'johnny'
is valid, but:
SELECT a.*, `as`.* FROM `fruit_db`.`apples` AS `a`
INNER JOIN `fruit_db`.`apple_seeds` AS `as` ON a.id = as.apple_id
WHERE (a.id = 1) AND (as.seed_name HAVING 'johnny')
which Zend produces is invalid SQL. I don't want just the one row that has seen_name 'johnny', i want ALL rows where apple id = 1 AND seed_name 'johnny' is somewhere in those results. Can I get what I need via Zend_Db_Select or do I need to go the raw query() route?
Edit: I've revised the question a bit to be closer to what I want and trying to clarify it a bit.