世界。
我这里有两个表叫做驱动程序和签入。 checkin表的d_id字段保存在drivers表中的条目的id。
在我的查询中,我试图加入表并在连接表上执行where条件。
到目前为止,我有这个查询(我无法加入表格,在我实现此目的后,联接表上的部分就出现了)
SELECT checkin.id, checkin.d_id, checkin.place, ( 3959 * acos( cos( radians(8.9551889) ) * cos( radians( lati ) )
* cos( radians( longi ) - radians(76.9902276) ) + sin( radians(8.9551889) ) * sin(radians(lati)) ) ) AS distance
FROM checkin WHERE status != 1
INNER JOIN drivers ON drivers.id = checkin.d_id
HAVING distance < 10
ORDER BY distance
LIMIT 0 , 20
此查询在没有内部联接的情况下工作,但我不确定我在这里做错了什么。如果有人能够指出它,那将会有所帮助。 感谢。
答案 0 :(得分:1)
您应该在where
之后使用inner join
子句:
SELECT checkin.id, checkin.d_id, checkin.place, ( 3959 * acos( cos( radians(8.9551889) ) * cos( radians( lati ) )
* cos( radians( longi ) - radians(76.9902276) ) + sin( radians(8.9551889) ) * sin(radians(lati)) ) ) AS distance
FROM checkin
INNER JOIN drivers ON drivers.id = checkin.d_id
WHERE status != 1
HAVING distance < 10
ORDER BY distance
LIMIT 0 , 20;
替代查询:
SELECT checkin.id, checkin.d_id, checkin.place, ( 3959 * acos( cos( radians(8.9551889) ) * cos( radians( lati ) )
* cos( radians( longi ) - radians(76.9902276) ) + sin( radians(8.9551889) ) * sin(radians(lati)) ) ) AS distance
FROM (select *from checkin where status!=1) checkin
INNER JOIN drivers ON drivers.id = checkin.d_id
HAVING distance < 10
ORDER BY distance
LIMIT 0 , 20;