带有lat lng和多个搜索连接的mysql查询

时间:2017-05-08 11:15:24

标签: php mysql

你好,我有mysql查询,因为它工作正常,但我想检查现在条件现在branches.visible = 1然后也在查询中显示

SELECT 
    company.name,
    company.id,
    company.mobile,
    company.telephone,
    IFNULL(fav.status, 'no') AS 'favorite',branchInfo.logo,
    ((((acos(sin(($lat*pi()/180)) * sin((branches.lat*pi()/180))+cos(($lat*pi()/180)) * cos((branches.lat*pi()/180)) * cos((($lng- branches.lng)*pi()/180))))*180/pi())*60*1.1515*1.609344)) AS 'distance',
    branchInfo.telephone,
    branchInfo.id,
    branchInfo.name,
    branches.buffet,
    branches.halal,
    branches.kidsPlayArea,
    branches.liveBand,
    branches.outdoor,
    branches.sheesha,
    branches.valetParking,
    branches.petsAllowed,
    branches.wiFi,
    branches.sportEvents,
    branches.reservationRequired,
    branches.wheelChairAccess,
    branches.acceptCreditCard,
    branches.brunch,
    branchInfo.name,
    branches.street,
    branches.city,
    branches.area,
    branches.lat,
    branches.lng,
    branches.id,
    branches.visible 
FROM branches
LEFT JOIN users branchInfo 
    ON branchInfo.id=branches.branchId 
LEFT JOIN users company 
    ON company.id=branches.companyId
LEFT JOIN favorite_branches fav 
    ON fav.branchId=branches.branchId AND fav.userId=$uId
HAVING distance < 25

1 个答案:

答案 0 :(得分:0)

使用where子句并在那里添加条件:

SELECT 
    company.name,
    company.id,
    company.mobile,
    company.telephone,
    IFNULL(fav.status, 'no') AS 'favorite',branchInfo.logo,
    ((((acos(sin(($lat*pi()/180)) * sin((branches.lat*pi()/180))+cos(($lat*pi()/180)) * cos((branches.lat*pi()/180)) * cos((($lng- branches.lng)*pi()/180))))*180/pi())*60*1.1515*1.609344)) AS 'distance',
    branchInfo.telephone,
    branchInfo.id,
    branchInfo.name,
    branches.buffet,
    branches.halal,
    branches.kidsPlayArea,
    branches.liveBand,
    branches.outdoor,
    branches.sheesha,
    branches.valetParking,
    branches.petsAllowed,
    branches.wiFi,
    branches.sportEvents,
    branches.reservationRequired,
    branches.wheelChairAccess,
    branches.acceptCreditCard,
    branches.brunch,
    branchInfo.name,
    branches.street,
    branches.city,
    branches.area,
    branches.lat,
    branches.lng,
    branches.id,
    branches.visible 
FROM branches
LEFT JOIN users branchInfo 
    ON branchInfo.id=branches.branchId 
LEFT JOIN users company 
    ON company.id=branches.companyId
LEFT JOIN favorite_branches fav 
    ON fav.branchId=branches.branchId AND fav.userId=$uId
WHERE ((((acos(sin(($lat*pi()/180)) * sin((branches.lat*pi()/180))+cos(($lat*pi()/180)) * cos((branches.lat*pi()/180)) * cos((($lng- branches.lng)*pi()/180))))*180/pi())*60*1.1515*1.609344)) < 25 
AND branches.visible = 1