2016-07-28 75 views
0

我試圖通過執行以下查詢的PHP應用程序來填充表:SQL錯誤在PHP返回的查詢與聯接

$sql3 = "SELECT distinct(`t1.testName`), `t2.comments AS C1` from `sample AS t1` left join `sample AS t2` ON `t1.testName`= `t2.testName` where `t1.buildNumber`= 181 and `t2.buildNumber`= 180 and `t1.errorStackTrace` is not null"; 

$result3 = mysqli_query($dbconnect,$sql3); 

if(!mysqli_query($dbconnect, $sql3)){ 
    printf("error message: %s\n",mysqli_error($dbconnect)); 
} 

我看到下面的錯誤返回:

error message: Table 'testdata.sample as t1' doesn't exist 

我已經嘗試了很多來解決這個問題,但不能。在MySQL上運行時查詢運行良好。 任何幫助,將不勝感激。 謝謝

+0

使用適當的反引號或從示例AS t1中刪除'$ sql3 =「SELECT distinct(t1.testName),t2.comments AS C1 left left join sample AS t2 ON t1.testName = t2.testName其中t1.buildNumber = 181和t2.buildNumber = 180和t1 .errorStackTrace不爲空「; ' –

回答

1

您必須使用反引號唯一的角落找尋表名或列名不包括別名:

$sql3 = "SELECT distinct(`t1`.`testName`), `t2`.`comments` AS C1 from `sample` AS ` left join `sample` AS t2 ON t1.testName= t2.testName where t1.buildNumber= 181 and t2.buildNumber= 180 and t1.errorStackTrace is not null"; 
+0

謝謝@Jens它的工作 – Saurabh

1

您正在錯誤地轉義表名稱。使用這個原始查詢:

SELECT DISTINCT(t1.testName), 
     t2.comments AS C1 
FROM `sample` AS t1 
LEFT JOIN `sample` AS t2 
    ON t1.testName = t2.testName 
WHERE t1.buildNumber = 181 AND 
     t2.buildNumber = 180 AND 
     t1.errorStackTrace IS NOT NULL 

我不認爲你真的需要任何反引號。但是在任何情況下,只有列名稱需要反撥,而不是別名,例如,

t1.`testName` but NOT `t1.testName` 
0

錯反引號,試試這個:

$sql3 = "SELECT distinct(t1.`testName`), t2.`comments` AS C1 from `sample` AS t1 left join `sample` AS t2 ON t1.`testName`= t2.`testName` where t1.`buildNumber`= 181 and t2.`buildNumber`= 180 and t1.`errorStackTrace` is not null";