2015-08-20 11:38:36 4146次浏览 4条回答 1 悬赏 10 金钱

例如:
一、
select u.username,count(a.id) num from user u left join article a on u.id = a.userid group by u.id order by num desc;
二、
/下面的查询user与userinfo一对一,user与userimage一对多/
select f.image from user u inner join userinfo f on u.id = f.userid and u.id = 123
union
select i.image from user u inner join userimage i on u.id = i.userid and u.id = 123;

类似于这些查询该怎么查?

最佳答案

  • naivefang 发布于 2015-08-20 21:43 举报

    yii 1

    Yii::app()->db->createCommand($sql)->queryAll();
    

    yii2

    Yii::$app->db->createCommand($sql)->queryAll();
    
  • 回答于 2015-08-20 22:56 举报

    这个有讲的更明白点的方法么,我配置关系感觉有点乱。最后直接写的sql语句!

    觉得很赞
  • 回答于 2015-08-21 01:27 举报

    如果只是简单关联也可以用 \yii\db\Query
    具体查看类手册 \yii\db\Query
    比如

    $data=(new \yii\db\Query())
                    ->select('cf_comment.*,cf_user_wx.nickname,cf_user_wx.headimgurl')
                    ->from('cf_comment')
                    ->where('time<'.$param['t'])
                    ->andWhere(['projectId'=>$param['pjId']])
                    ->andWhere(['type'=>1])
                    ->leftJoin('cf_user_wx','cf_comment.userId = cf_user_wx.id')
                    ->offset($start)
                    ->orderBy(['time'=>SORT_DESC])
                    ->limit($count)
                    ->all();
    
  • 回答于 2015-08-21 10:27 举报

    Yii 可以组合任何复杂的语句,但是你不要控制好Yii自动生成的括号,因为这会导致结果不一样。

您需要登录后才可以回答。登录 | 立即注册
zhangseu77
实习生

zhangseu77

注册时间:2015-08-19
最后登录:2015-08-21
在线时长:0小时37分
  • 粉丝1
  • 金钱0
  • 威望0
  • 积分0

热门问题