Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge these 2 MySQL query into one
    primarykey
    data
    text
    <p>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.</p> <p>I want to show all listing ordered by distance between actual point (geolocalised, I have lat/lng) and cities in the listing table.</p> <p>Actualy, I do this with 2 MySQL queries, and it's work :</p> <pre><code>$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-&gt;db_city." WHERE $formula&lt;='$distance' ORDER by dist ASC"; $q = $this-&gt;db-&gt;query($q); $resultSQL = $q-&gt;result_array(); </code></pre> <p>And this one :</p> <pre><code>if ($localization != ''){ if ($whereClause == ''){ //$whereClause .= ' WHERE address_city LIKE "'.$localization.'"'; $loc = ''; foreach ($localization as $key =&gt; $value) { if ($loc == ''){ $loc .= '"'.$value.'"'; }else{ $loc .= ', "'.$value.'"'; } } $whereClause .= ' WHERE address_city IN ('.$loc.')'; }else{ //$whereClause .= ' &amp;&amp; address_city LIKE "'.$localization.'"'; $loc = ''; foreach ($localization as $key =&gt; $value) { if ($loc == ''){ $loc .= '"'.$value.'"'; }else{ $loc .= ', "'.$value.'"'; } } $whereClause .= ' &amp;&amp; address_city IN ('.$loc.')'; } } $q = "SELECT * FROM ".$this-&gt;db_listing.$whereClause." ORDER BY created_stamp DESC"; </code></pre> <p>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.</p> <p>I tried with LEFT JOIN, but it didn't work. Can you help me please ? Thanks !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload