2015-05-26 81 views
0

我試圖做一個數據庫查詢包括與yii2先進的框架聯接Yii2查詢 - 加入MySQL查詢中出現兩次

$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(V2.0.4)是:

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 

我的代碼有什麼問題? 爲什麼連接出現兩次?

回答

0

大概在where(['name' => $client])'name'字段不存在於Heartbeat表,但它確實Client,並且INNER JOIN添加由於Yii的

檢測表關係