简体   繁体   English

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

[英]MySQL JOIN pass PHP variable to two tables

I have the following MySQL query in PHP that passes a variable to complete the query: 我在PHP中有以下MySQL查询,该查询传递变量以完成查询:

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

I have the following MySQL JOIN that provides data from two tables: 我有以下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

I need to combine these two queries into one with a JOIN and still passing the $uid variable to complete the query. 我需要将这两个查询与一个JOIN合并为一个,并仍然传递$ uid变量来完成查询。

I've tried the following and it did not work: 我已经尝试了以下方法,但没有效果:

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'

Your query will return a cross product between the mobile_tech and gbl_emplisting rows for $uid . 您的查询将返回之间的交叉产品mobile_techgbl_emplisting行为$uid If you just want one row, as in the first query, use ORDER BY and LIMIT similarly. 如果只需要一行,如在第一个查询中一样,则类似地使用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

Please try with this and say the result 请尝试这个并说出结果

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 inner join gbl_qemplisting
        on mobile_tech.uid=gbl_qemplisting.EmpNo 
where mobile_tech.uid=$uid AND date(timestamp)='$currentday'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM