-1

不工作:

$sql = new Sql($this->adapter);

$select = $sql->select();
$select->from('请求')
           ->columns(array('*', new Expression("CONCAT(up1.value,' ',up2.value) as display_name")))
            ->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
           ->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')
;

返回$选择;

怎么做对?

4

2 回答 2

0

您为两个连接指定了字段:

        ->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
       ->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')

当查询转换为实际的 sql 时,这些字段将自动命名空间,因此您将在字段列表中获得“up1.up1.value”。

从连接中删除字段引用,它应该可以工作。

UPD。对,还有更多。您不能将 'user_first_name' 作为字符串值传递给 join 的“on”参数,因为它将被解释为列名。所以你必须传递一个表达式:

    $select = $sql->select();
    $select->from('request')
           ->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_name')));

    $expressionString = '? = ? AND ? = ?';
    $types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);

    $parameters = array('request.user_id', 'up1.user_id', 'up1.key', 'first_name');
    $expression1 = new Expression($expressionString, $parameters, $types);

    $parameters = array('request.user_id', 'up2.user_id', 'up2.key', 'last_name');
    $expression2 = new Expression($expressionString, $parameters, $types);

    $select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
           ->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');
于 2013-07-14T08:34:51.053 回答
0

Y 三到:

$select = $sql->select();
$select->from('request')
           ->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_user_name')));

$expressionString = '? = ? AND ? = ?';
$types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);

$parameters1 = array('request.user_id', 'up1.user_id', 'up1.key', 'user_first_name');
$expression1 = new Expression($expressionString, $parameters1, $types);

$parameters2 = array('request.user_id', 'up2.user_id', 'up2.key', 'user_last_name');
$expression2 = new Expression($expressionString, $parameters2, $types);

$select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
           ->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');

不起作用,错误在哪里

于 2013-07-17T15:04:53.770 回答