在mysqli中使用subselect

时间:2016-12-28 20:00:00

标签: mysql

我有一个当前有效的查询,但我们增加了一个客户进行多个测试驱动的可能性。这个额外的测试驱动器在另一个表上。客户ID是testdriveform表上的自动增量编号。因此,我需要根据表2中表1中的客户ID检查添加条目,并将其与其余条目一起列出。

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。任何人吗?

1 个答案:

答案 0 :(得分: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;