MySQL JOIN将PHP变量传递给两个表

时间:2016-06-03 18:56:02

标签: php mysql

我在PHP中有以下MySQL查询,它传递一个变量来完成查询:

SELECT * from mobile_tech WHERE uid=$uid  order by timestamp DESC limit 0,1

我有以下MySQL JOIN,它提供来自两个表的数据:

SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName 
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=gbl_qemplisting.EmpNo AND date(timestamp)='$currentday' 
group by uid

我需要将这两个查询合并为一个JOIN并仍然传递$ uid变量来完成查询。

我尝试过以下操作但没有用:

SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName 
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=$uid AND gbl_qemplisting.EmpNo=$uid AND date(timestamp)='$currentday'

2 个答案:

答案 0 :(得分:1)

您的查询将返回mobile_tech行的gbl_emplisting$uid行之间的叉积。如果您只想要一行,就像在第一个查询中一样,请同样使用ORDER BYLIMIT

SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName 
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=$uid AND gbl_qemplisting.EmpNo=$uid AND date(timestamp)='$currentday'
ORDER BY mobile_tech.timestamp
LIMIT 1

答案 1 :(得分:1)

请尝试使用此功能并说出结果

c