有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']);
最佳答案
-
johnny1991 发布于 2017-04-14 17:04 举报
self::find()
->alias('t1') ->select($selector) ->leftJoin(self::EmployeeBasicInformationTableName . ' t2', 't1.created_uuid = t2.uuid');
这样试试
其他 0 个回答
没有找到数据。
阿江
注册时间:2015-10-18
最后登录:2024-03-03
在线时长:186小时21分
最后登录:2024-03-03
在线时长:186小时21分
- 粉丝94
- 金钱16816
- 威望160
- 积分20276