簡體   English   中英

如何在沒有括號的Zend_Db_Select語句中使用“HAVING”子句?

[英]How do I use a “HAVING” clause in a Zend_Db_Select statement without parenthesis?

我知道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'

不是“HAVING(as.seed_name ='johnny')”

回溯一下,我們有表:

fruit_db.apples

| id  | name |
--------------
|  1  | red  |
|  2  | green|

fruit_db.apple_seeds

| 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是無效的。 我不希望只有一行看到了'johnny',我想要所有行,其中apple id = 1 AND seed_name'johnny'就在這些結果的某個地方。 我可以通過Zend_Db_Select獲得我需要的東西,還是需要進行原始查詢()路由?

編輯:我已經修改了一些問題,以便更接近我想要的東西,並試圖澄清一下。

更改

- > where('as.apple_id HAVING 1');

- >有('as.apple_id = 1');

http://framework.zend.com/manual/en/zend.db.select.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM