如何使用“HAVING” Zend_Db_Select 语句中不带括号的子句?
我知道 Zend 提供了having()方法,但我想要的是一个像这样的查询:
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'
')”
回溯一下,我们有表:
fruit_db.applesfruit_db.apple_seeds
| id | name |
--------------
| 1 | red |
| 2 | green|
而不是“HAVING (as.seed_name = ' johnny
| apple_id | seed_name |
------------------------
| 1 | johnny |
| 1 | judy |
| 2 | granny |
我想要结果如下:
| id | name | apple_id | seed_name |
-------------------------------------
| 1 | red | 1 | johnny |
| 1 | red | 1 | judy |
上面提供的查询给出了这个结果,但是使用 Zend_Db_Select 在having和where语句的每个部分周围加上括号,这使得我的查询无效。因此
$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"');
产生:
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')
这是无效的 SQL。简而言之:
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'
是有效的,但是:
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')
Zend 生成的是无效的 SQL。我不想要只包含 saw_name 'johnny' 的一行,我想要 apple id = 1 且 seed_name 'johnny' 位于这些结果中某处的所有行。我可以通过 Zend_Db_Select 获得我需要的东西还是我需要走原始 query() 路线?
编辑:我对问题做了一些修改,使其更接近我想要的内容,并试图澄清一下。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
->where('as.apple_id HAVING 1');
到
->having('as.apple_id = 1');
http://framework.zend.com/manual/en/zend.db .select.html
change
->where('as.apple_id HAVING 1');
to
->having('as.apple_id = 1');
http://framework.zend.com/manual/en/zend.db.select.html