2016-12-28 63 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