Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join a subquery using Zend_Db_Select

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
like image 641
Oleg Avatar asked Jan 17 '11 16:01

Oleg


1 Answers

$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)
;
like image 170
Stephen Fuhry Avatar answered Nov 01 '22 16:11

Stephen Fuhry