How can I join a subquery using Zend_Db_Select

纵饮孤独 提交于 2019-12-05 18:01:36

问题


How would I construct this query using Zend_Db_Select?:

SELECT users.user_id, email_address, t1.value as 'languages'    
FROM users
LEFT JOIN (
  SELECT
    user_id
    , field_id
    , GROUP_CONCAT(value SEPARATOR ',') AS value
  FROM user_multivalued
  WHERE field_id=25
  GROUP BY user_id, field_id) t1
ON t1.user_id = users.users_id
WHERE list_id = 45

回答1:


$user_multivalued = $db
    ->select()
    ->from('user_multivalued', array(
        'user_id',
        'field_id',
        new Zend_Db_Expr("GROUP_CONCAT(value SEPARATOR ',') AS value")
    ))
    ->where('field = ?', 25)
    ->group('user_id')
    ->group('field_id')
;

$select = $db
    ->select()
    ->from('users', array('user_id', 'email_address'))
    ->joinLeft(
        array('t1' => $user_multivalued),
        't1.user_id = users.user_id',
        array('languages'=>'value')
    )
    ->where('list_id = ?', 45)
;


来源:https://stackoverflow.com/questions/4715718/how-can-i-join-a-subquery-using-zend-db-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!