2014-04-19 111 views
0
$xyz = mysql_fetch_array(mysql_query('select sum(value) points where userId = $userIdDB'))['suma']; 

查詢將如何在zend框架中查找?我需要從數據庫中選擇總和記錄爲int。 另一個問題:我可以做MySQL查詢。我實際上沒有任何來自zend的知識,所以我請詳細解釋。 在zend中的mysql連接呢?Zend Framework - mysql查詢

回答

0

In ZEND

$select = $db->select() 
      ->from('points',array(new Zend_Db_Expr('sum(value)'))) 
      ->where('userId = ?', $userIdDB); 

當添加表達列

列在SQL查詢有時表情,從表中不是簡單地列 名。表達式不應具有相關名稱或 適用的引用。如果您的列字符串包含圓括號,則 Zend_Db_Select將其識別爲表達式。

您還可以顯式創建Zend_Db_Expr類型的對象,以 防止將字符串視爲列名稱。 Zend_Db_Expr是 一個包含單個字符串的最小類。 Zend_Db_Select 可識別類型爲Zend_Db_Expr的對象,並將它們轉換回 字符串,但不會應用任何更改(如引用或 相關名稱)。

EXAMPLE IN ZEND

// Build this query using Zend_Db_Expr explicitly: 
// SELECT p."product_id", p.cost * 1.08 AS cost_plus_tax 
// FROM "products" AS p 

$select = $db->select() 
      ->from(array('p' => 'products'), 
        array('product_id', 
          'cost_plus_tax' => 
           new Zend_Db_Expr('p.cost * 1.08')) 
        ); 
+0

,將返回總和(值),或表的一個int值? – o0skar

+0

我現在修好了,試試吧 – underscore

+0

能工作嗎? – underscore