谁一开始就是大神?

有joinWith()的SQL查询怎么写select()语句?[2.0]

我的原始语句是:

$query = ArticlesSearhch::find()->where(['isDeleted'=>0])->joinWith(['authors']);

因为article中的content字段内容很多,所以我现在不想全查出来,只查出title、authorId、lastEdited等几个字段,这个select()语句该怎么写?

ArticlesSearhch中的authors

    public function getAuthors()
    {
        return $this->hasOne(TbAuthors::className(), ['id' => 'authorId'])
            ->asArray();
    }

我改写成下面这样,报错:

$query = ArticlesSearhch::find()->select('articles.id','articles.title','articles.authorId','articles.lastEdited','articles.format')->where(['articles.isDeleted'=>0])->from(['articles'=>'articles'])->joinWith(['authors']);

报错信息:

Database Exception – yii\db\Exception

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(*) FROM `articles` `articles` LEFT JOIN `authors` ON `articles`.`authorId`' at line 1
The SQL being executed was: SELECT articles.title COUNT(*) FROM `articles` `articles` LEFT JOIN `authors` ON `articles`.`authorId` = `authors`.`id` WHERE `articles`.`isDeleted`=0

求大神帮忙!谢谢!

搞定,格式写错了,改成这样就可以了:

$query = ArticlesSearhch::find()->select(['articles.id','articles.title','articles.authorId','articles.lastEdited','articles.format'])->where(['articles.isDeleted'=>0])->from(['articles'=>'articles'])->joinWith(['authors']);

self::find()
这样试试

赞(0) 打赏
未经允许不得转载:菜鸟之家 » 有joinWith()的SQL查询怎么写select()语句?[2.0]

评论 抢沙发

登录

找回密码

注册