2016-12-28 64 views
0

我有一個當前可用的查詢,但我們增加了一個客戶做多個測試驅動的可能性。這個額外的測試驅動器在不同的桌子上。客戶ID是testdriveform表上的自動增量編號。因此,我需要根據表2中表1的客戶ID檢查添加條目,並與其餘部分一起列出。在mysqli中使用子查詢

SELECT * FROM testdriveform Where SalesAgent not like '%Test%' and SalesAgent like '%$Sales%' and Date between '$sDate' and '$eDate' and Stock != '' order by Date, SalesAgent; 

我需要添加一個子選擇,我認爲。增加的東西

SELECT * FROM testdrives WHERE custID = testdriveform.id 

phpmyadmin在bluehost..again下。任何人?

+0

我想你只需要'JOIN'這兩個表 –

回答

1

不,你不需要子選擇;而所需的表之間的JOIN

SELECT td.*, tdf.* 
FROM testdriveform tdf 
JOIN testdrives td ON td.custID = tdf.id 
Where tdf.SalesAgent not like '%Test%' 
and tdf.SalesAgent like '%$Sales%' 
and tdf.`Date` between '$sDate' and '$eDate' 
and tdf.Stock != '' 
order by tdf.`Date`, tdf.SalesAgent; 
+0

您使用TDF和TD升技在那裏,是東西,創建一個類型的簡寫,還是我用實際的表名替換這些東西?另外,謝謝。我正在給它一個 – Griehle

+0

@Griehle,是的,這些是表別名,以便更好的可讀性 – Rahul

+0

行。所以這只是返回第二個表中的行。但我感覺它很接近。同樣感謝你的速記。 – Griehle