3

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

不是“有(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 将括号放在有和 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 是无效的。我不希望只看到 see_name 'johnny' 的一行,我想要其中 apple id = 1 并且 seed_name 'johnny' 在这些结果中某处的所有行。我可以通过 Zend_Db_Select 获得我需要的东西,还是需要走原始的 query() 路线?

编辑:我已经修改了这个问题,使其更接近我想要的,并试图澄清一下。

4

1 回答 1

1

改变

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

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

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

于 2011-10-28T13:23:22.933 回答