MySQL IN只返回单个记录

时间:2014-05-06 06:57:42

标签: php mysql cakephp sum

您好我正在使用CakePHP版本2.x我遇到了一个问题。请检查我的代码

points

id    point
-----------
1       5
2       6
3       1
4       5

代码:

$myPoint = $this->find("first",array('conditions'=>array('user_id'=>CakeSession::read('Auth.User.id')),'fields'=>array('point','active_column')));
echo $myPointList = $myPoint['User_point']['active_column'];

输出:2,3

$Point = ClassRegistry::init('Point');
$Point->virtualFields['point'] = 'SUM(Point.point)';
$getPoints = $Point->find('first', array(
                             'conditions'=>array(
                             'Point.id'=>array($myPointList)),'fields' => array(
                             'point')));
echo $getPoints['Point']['point']; exit;

在上面的输出中,点的2和3之和为6+1=7

但我只得到6为什么?

修改

$myPointList是动态的,如果我在那里使用静态,结果显示完美。请检查以下代码

代码:

$getPoints = $this->Point->find('first', array(
                                 'conditions'=>array(
                                 'Point.id'=>array(2,3)),'fields' => array(
                                 'point')));

3 个答案:

答案 0 :(得分:1)

问题是$myPointList是一个字符串。试试这段代码

$getPoints = $Point->find(
    'all', 
    array(
        'conditions'=>array(
            'Point.id IN (?)'=> $myPointList,
         )
        'fields' => array (
            'point'
        )
    )
);

或者您可以使用$myPointList

explode转换为数组
$myPointList =  explode(',', $myPointList);

$getPoints = $Point->find(
    'all', 
    array(
        'conditions'=>array(
            'Point.id'=> $myPointList,
         )
        'fields' => array (
            'point'
        )
    )
);

您可以使用find('first'),但如果您使用find('all')则会更好。使用find('first')只需在生成的查询中添加'LIMIT 1'即可。这不会影响您的结果,因为在LIMIT之后应用了SUM。但这是不必要的,所以我认为你可以避免使用它

编辑:阅读@ AD7six

中的评论后

您最好使用field()代替find()。所以你的代码变成了

$getPoints = $Point->field(
    'SUM(Point.point)', 
     array(
        'Point.id IN (?)'=> $myPointList
    )
);

或者

$myPointList =  explode(',', $myPointList);

$getPoints = $Point->field(
    'SUM(Point.point)', 
     array(
        'Point.id'=> $myPointList
    )
);

答案 1 :(得分:0)

CakePhp IN允许数组(),但你的数据返回正常文本,如 2,3 我已使用explode()函数将字符串转换为数组。

$arrryMypoint=explode(",",$myPointList);请更新您的代码,让我知道它的工作情况?

请使用此代码:

$myPoint = $this->find("first",array('conditions'=>array('user_id'=>CakeSession::read('Auth.User.id')),'fields'=>array('point','active_column')));
$myPointList = $myPoint['User_point']['active_column'];

$arrryMypoint=explode(",",$myPointList);


$Point = ClassRegistry::init('Point');
$Point->virtualFields['point'] = 'SUM(Point.point)';
$getPoints = $Point->find('first', array(
                             'conditions'=>array(
                             'Point.id'=>$arrryMypoint),'fields' => array(
                             'point')));
echo $getPoints['Point']['point']; exit;

答案 2 :(得分:-2)

如果你先使用那么它只返回第一条记录,在你的情况下输出只是id 2的输出,所以使用所有的第一个

$myPoint = $this->find("all",array('conditions'=>array('user_id'=>CakeSession::read('Auth.User.id')),'fields'=>array('point','active_column')));

echo $myPointList = $myPoint['User_point']['active_column']; 


   $getPoints = $Point->find('all', array(
                                 'conditions'=>array(
                                 'Point.id'=>array($myPointList)),
                                 'fields' => array('SUM(Point.point)')
                                  )
                             );