本文介紹了如何在 Zend 框架中使用 GROUP_CONCAT?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
假設我有一張桌子:學生
Assume that I have a table : students
______________________________________________________
|id | name | school | class |
______________________________________________________
| 1 | John | ABC | C1 |
| 2 | Jack | ABC | C1 |
| 3 | Anna | ABC | C1 |
| 4 | Peter | DEF | D1 |
| 5 | Alex | ABC | C2 |
| 6 | Bryan | ABC | C2 |
| 7 | David | ABC | C2 |
| 8 | Cristian | DEF | D1 |
_______________________________________________________
使用此查詢:
SELECT a.class,GROUP_CONCAT(a.name) as names FROM students a WHERE a.school='ABC' GROUP BY a.class
給我這個結果:
____________________________
|class | names |
____________________________
| C1 | John, Jack, Anna |
| C2 | Alex, Bryan, David|
____________________________
如何在 Zend Framework 中使用 Zend_Db_Table 或 Zend_Db_Select 執行此查詢?非常感謝!
How can I execute this query in Zend Framework by using Zend_Db_Table or Zend_Db_Select? Thank you so much!
推薦答案
我想它會是這樣的.試試吧.
I guess it will be something like this. Try it.
$table = Your_DbTable_Class();
$select = $table->select()
->setIntegrityCheck(false)
->from(array('a' => 'students'), array( 'class' => 'class' , 'names' => new Zend_Db_Expr('GROUP_CONCAT(a.name)')) )
->where( 'a.school = ?', 'ABC' )
->group('a.class');
當我組裝它時,它給了我以下查詢:
When I assemble it, it gives me following query:
SELECT `a`.`class`, GROUP_CONCAT(a.name) AS `names` FROM `students` AS `a`
WHERE (a.school = 'ABC')
GROUP BY `a`.`class`
這是您要找的嗎?
這篇關于如何在 Zend 框架中使用 GROUP_CONCAT?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!