[英]combine 2 select queries in mysql
I have 2 select statements: 我有2条选择语句:
timestamp of emp getting awards for specific emp id emp的时间戳获得特定emp id的奖励
SELECT * FROM user_table,employeetable,awards where user_table.empid=employeetable.empid AND user_table.empid=awards.empid AND user_table.empid=123 ORDER BY timestamp DESC
All employees staying around 25 miles from the current loc: current location: lat =37 lng=-122
所有员工距
current location: lat =37 lng=-122
约25英里: current location: lat =37 lng=-122
SELECT * ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM user_table,employeetable,awards where user_table.empid=employeetable.empid AND user_table.empid=awards.empid HAVING distance < 25 ORDER BY distance;
How do I combine both and ORDER BY timestamp ?btw both have field timestamp. 如何将两者和ORDER BY时间戳结合在一起?btw都具有字段时间戳。
1.has specific user 1.有特定的用户
2.all users within specific radius 2.特定半径内的所有用户
I really appreciate any help.Thanks in Advance. 非常感谢您的帮助。
You can combine the two queries into a single query, just using logic in the where
clause (which this has turned into a having
clause: 您可以使用
where
子句中的逻辑将这两个查询组合为一个查询(已将其转换为having
子句:
select *, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) as distance
from user u join
employee e
on u.empid = e.empid join
awards a
on u.empid = a.empid
having empid = 123 or distance < 25;
This uses having
instead of where
so the distance
column alias can be used instead of the formula. 这使用了
having
而不是where
因此可以使用distance
列别名代替公式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.