鍍金池/ 問(wèn)答/PHP/ 運(yùn)動(dòng)日期去重的問(wèn)題?

運(yùn)動(dòng)日期去重的問(wèn)題?

返回給前端運(yùn)動(dòng)日期,年月日不能重復(fù):
public function getMemberPhysicalTestDate()
{
    $arr = MemberPhysicalTest::find()
        ->alias('mpt')
        ->joinWith(['member m'],FALSE)
        ->where([
            'm.member_account_id' => $this->accountId,
            'mpt.type' => $this->type,
            'mpt.is_delete' => 0
        ])
        ->select('mpt.create_at')
        ->groupBy(["DATE_FORMAT(from_unixtime(mpt.create_at),'%Y-%m-%d')"])
        ->orderBy('mpt.create_at desc')
        ->asArray()
        ->all();
    return $arr;
}

clipboard.png

clipboard.png
這個(gè)方法不太好使:查詢出來(lái)的只有第一條,第二條6月26號(hào)的沒(méi)有查出來(lái)。

回答
編輯回答
敢試
public function getMemberPhysicalTestDate()
{
    $arr = MemberPhysicalTest::find()
        ->alias('mpt')
        ->joinWith(['member m'],FALSE)
        ->where([
            'm.member_account_id' => $this->accountId,
            'mpt.type' => $this->type,
            'mpt.is_delete' => 0
        ])
        ->select(["DATE_FORMAT(mpt.create_at,'%Y-%m-%d') as testDate"])
        ->groupBy(["DATE_FORMAT(mpt.create_at,'%Y-%m-%d')"])
        ->orderBy('mpt.create_at desc')
        ->asArray()
        ->all();
    return $arr;
}
這里在groupBy分組的時(shí)候由于此處的日期是日期格式,而不是時(shí)間戳,所以用from_unixtime就多此一舉了,去掉就ok了。
2018年6月27日 18:57