2015-02-06 107 views
1

我有以下SQL SELECT:合并Zend框架2

SELECT c.id, c.code, c.closed, COALESCE(c2.name, c.name) AS name 
FROM `centers` c 
LEFT JOIN `centers_i18n` c2 ON c2.center_id = c.id 
AND c2.lang = 'ES' 

在我的Zend应用Y具有以下选择:

$select = $sql->select(); 
    $select->from($this->table); 
    $select->join($this->multiLangTable, $this->table . '.id=' .$this->multiLangTable . '.' . $this->foreignKey, $mapping); 
    $select->where($conditions); 

这是一样的,但我不知道该怎么从列名称合并。

回答

2

您必须指定要选择并添加一个为表达列

$select->columns(
    array(
    ... 
    'name' => new \Zend\Db\Sql\Expression('COALESCE(c2.name, c.name)') 
    ... 
) 
); 

整个代码应该是接近这个(我还没有与Zend工作了一段时间,所以不可能有错误这里):

$select = $sql->select(); 
    $select->columns([ 
    'id' => 'id', 
    'code' => 'code', 
    'closed' => 'closed', 
    'name' => new \Zend\Db\Sql\Expression('COALESCE(c2.name, c.name)') 
    ]); 
    $select->from(['c' => $this->table]); 
    $select->join(['c2' => $this->multiLangTable], 'c.id = c2.' . $this->foreignKey, $mapping); 
    $select->where($conditions);