2020-01-02 15:25:03 1466次浏览 2条回答 0 悬赏 20 金钱
static public function getObj($where='',$andwhere='') {
    $ykck = self::find()
        ->alias('p')
        ->select(['p.*', 'c.OrderID','c.GoodsTotalPrice cGoodsTotalPrice', 'c.agentPrice cagentPrice','c.GoodsCount','c.OverPrice cOverPrice','c.SendPrice cSendPrice','c.SendStatus','c.userRemark','c.SendEmail','c.SendEmailTime','c.EmailRecipienter','c.EmailContent','c.DeliveryDate'])
        ->rightJoin(ShopOrderChild::tableName().' as c','p.MainOrder = c.MainOrderID')
        ->where($where)
        ->Andwhere($andwhere)
        ->orderBy(['p.CreateTime'=>SORT_DESC]);
    return $ykck;
}

 $ykck = self::getObj($where);
 $cnt = $ykck->count();
 $pager = new Pagination(['totalCount' => $cnt, 'pageSize' => $pageSize]);
 $cks = $ykck->offset($pager->offset)->limit($pager->limit)->asArray()->all();

通过 model 类执行之后获取的数据数量为 9

通过解析 SQL 语句并在 navicat 中执行数据数量为 10

通过 createCommand 执行原生 SQL 语句数据数量为 10

SQL语句如下:

    SELECT
	`p`.*,
	`c`.`OrderID`,
	`c`.`GoodsTotalPrice` AS `cGoodsTotalPrice`,
	`c`.`agentPrice` AS `cagentPrice`,
	`c`.`GoodsCount`,
	`c`.`OverPrice` AS `cOverPrice`,
	`c`.`SendPrice` AS `cSendPrice`,
	`c`.`SendStatus`,
	`c`.`userRemark`,
	`c`.`SendEmail`,
	`c`.`SendEmailTime`,
	`c`.`EmailRecipienter`,
	`c`.`EmailContent`,
	`c`.`DeliveryDate` 
    FROM
	`yk_shop_order` `p`
	RIGHT JOIN `yk_shop_order_child` `c` ON p.MainOrder = c.MainOrderID 
    WHERE
	1 = 1 
    ORDER BY
	`c`.`CreateTime` DESC 
	LIMIT 10

这个问题请教一下,为什么会出现这个问题?有什么解决方案呢?

  • 回答于 2020-01-02 18:45 举报

    日常查询都是用 Query 去查询,而非使用AR去查询,因为 AR 曾经出现过丢失数据的情况,但是未曾深入研究过。

  • 回答于 2020-01-03 09:31 举报

    提个思路不知道楼主用过没。
    把yii翻译出的sql在debug里找出来,用这个去数据库里执行看看。

    1 条回复
    回复于 2020-01-04 09:54 回复

    sql语句在数据库中执行是正常的,问题已经找出来了:是因为主键重复的问题

您需要登录后才可以回答。登录 | 立即注册
JoeHandsome
副董事长

JoeHandsome 北京

注册时间:2017-10-10
最后登录:8小时前
在线时长:52小时29分
  • 粉丝9
  • 金钱34370
  • 威望50
  • 积分35390

热门问题