简体   繁体   中英

Merge these 2 MySQL query into one

I explain you the problem. I have two tables : "listing" who contain all my lists with different informations and particulary one : a city ID. I have a second table "city" who contain the complete list of all cities of my countries with many informations : latitude, longitude, city name, etc.

I want to show all listing ordered by distance between actual point (geolocalised, I have lat/lng) and cities in the listing table.

Actualy, I do this with 2 MySQL queries, and it's work :

$formula = "(6366*acos(cos(radians($latitude))*cos(radians(`latitude`))*cos(radians(`longitude`) -radians($longitude))+sin(radians($latitude))*sin(radians(`latitude`))))";

        $q = "SELECT *,$formula AS dist FROM ".$this->db_city." WHERE $formula<='$distance' ORDER by dist ASC";
        $q = $this->db->query($q);
$resultSQL = $q->result_array();

And this one :

if ($localization != ''){
            if ($whereClause == ''){
                //$whereClause .= ' WHERE address_city LIKE "'.$localization.'"';
                $loc = '';
                foreach ($localization as $key => $value) {
                    if ($loc == ''){
                        $loc .= '"'.$value.'"';
                    }else{
                        $loc .= ', "'.$value.'"';
                    }
                }
                $whereClause .= ' WHERE address_city IN ('.$loc.')';
            }else{
                //$whereClause .= ' && address_city LIKE "'.$localization.'"';
                $loc = '';
                foreach ($localization as $key => $value) {
                    if ($loc == ''){
                        $loc .= '"'.$value.'"';
                    }else{
                        $loc .= ', "'.$value.'"';
                    }
                }
                $whereClause .= ' && address_city IN ('.$loc.')';
            }
        }

        $q = "SELECT * FROM ".$this->db_listing.$whereClause." ORDER BY created_stamp DESC";

It's work, but the problem is I don't have the "dist" parameter accessible in this second query, so I can't order by dist. The solution for that is to merge the first big query (the one with the formula) with the second.

I tried with LEFT JOIN, but it didn't work. Can you help me please ? Thanks !

Try this, it will going to return you all the cities those with in the radius of $distance from provided longitude and latitudes.

"SELECT *,(((acos(sin(($latitude*pi()/180)) * sin((`latitude`*pi()/180))+cos(($latitude*pi()/180)) * cos((`Latitude`*pi()/180)) * cos((($longitude- 
    `longitude`)*pi()/180))))*180/pi())*60*1.1515) AS distance FROM location WHERE (((acos(sin(($latitude*pi()/180)) * sin((`latitude`*pi()/180))+cos(($latitude*pi()/180)) * cos((`Latitude`*pi()/180)) * cos((($longitude- 
    `longitude`)*pi()/180))))*180/pi())*60*1.1515) <= $distance"

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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