Yii2查询 - 连接在MySQL查询中出现两次

时间:2015-05-26 19:15:42

标签: mysql yii2 yii2-advanced-app

我尝试进行数据库查询,包括与yii2高级框架(v2.0.4)的连接

  $query = new \yii\db\Query;
  $res = $query->select('date')->from(Heartbeat::tableName())
       ->join('INNER JOIN', Client::tableName(), 'id = client_id')
       ->where(['name' => $client])
       ->orderBy('date DESC')->limit(1)->scalar();

正在执行的SQL是:

 SELECT `date` FROM `heartbeat`  
 INNER JOIN `client` ON id = client_id 
 INNER JOIN `client` ON id = client_id  
 WHERE `name`='CLIENT01' ORDER
 BY `date` DESC LIMIT 1

我的代码出了什么问题? 为什么联接会出现两次?

1 个答案:

答案 0 :(得分:0)

'name'表中的where(['name' => $client])字段可能不在Heartbeat表中,但在Client中却存在,并且由于检测到表关系而添加了INNER JOIN由Yii