简体   繁体   中英

need help creating a mysql query

Let's assume I a have a table called "users" which has the following relevant columns:

  • username
  • zipcode

Let's also assume I have a table called "shops" which has the following relevant columns:

  • shop_name
  • zipcode

Let's also assume I have a table called "preferred_shops" which has the following relevant columns:

  • shop_name
  • username

Let's also assume I have a table called "zipData" which has the following relevant columns:

  • zipcode
  • lat
  • lon

Currently I can run this code with success:

$lat="49.886436"; // Value should be obtained from the zipData table by doing a "SELECT FROM zipData WHERE zipcode=(users_zip_code)" or similar query 
$lon="-97.14553"; // Value should be obtained from the zipData table by doing a "SELECT FROM zipData WHERE zipcode=(users_zip_code)" or similar query
$radius=5;

$query="SELECT zipcode FROM zipData
    WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+
           POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius)";

The query above will successfully display all the other zip codes that are within the given radius of the supplied lat/lon but I have no interest in knowing what other zip codes there are... I want to know what shops are within the radius.

Any help you can give would be greatly appreciated.

==== Edited because I realized it is actually a little more complex ====

This is what I need to do...

  • Retrieve the user's zipcode from "users" table
  • Find the user's lat/lon from the zipData table
  • Find all the shops within a given radius that is also in a "preferred_shops" table with the given user in the "username" column

Problems to consider: the zipcode in "zipData" has no white space but the zipcodes in "users" and "shops" have white space for cosmetic reasons... such as Canadian Postal codes which are in the "A1A 1A1" format

==== Edited to post solution ====

The system says I am not allowed to answer my own question. That sounds strange since with the help of others I already found the answer. As a work around I am editing the original post. Here is the solution...

Ok so I figured it out (with the help of the people that replied)... This is what I did.

IT LIKELY COULD BE A LOT SHORTER AND CLEANER SO PLEASE FEEL FREE TO MAKE IT BETTER IF YOU KNOW A BETTER WAY.

$query = "SELECT * FROM preferred_shops
    WHERE `username`='{$_SESSION['account_name']}'
";

$result = mysql_query($query);
if (mysql_errno())
{ 
    die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) );
}

$num_rows = mysql_num_rows($result);

while($row = mysql_fetch_array($result))
{



    // Get user's preferences and postal code
    $query2 = "SELECT * FROM seekers
        WHERE `username`='{$_SESSION['account_name']}'
    ";

    $result2 = mysql_query($query2);
    if (mysql_errno())
    { 
        die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) );
    }
    $num_rows2 = mysql_num_rows($result2);

    $row2 = mysql_fetch_array($result2);

    $radius=$row2['radius']; // Didn't mention that is column was in the table but that didn't matter... the value could have come from anywhere.



    // Get user's lat/lon
    // Remove white space from the postal code
    $query3="SELECT * FROM zipData WHERE zipcode=replace('{$row2['postal']}',' ','')";
    $result3 = mysql_query($query3);
    if (mysql_errno())
    { 
        die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) );
    }
    $num_rows3 = mysql_num_rows($result3);

    $row3 = mysql_fetch_array($result3);

    $lat=$row3["lat"];
    $lon=$row3["lon"];



    $query4="SELECT shop_name FROM zipData,shops
        WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius)
        AND replace(shops.zipcode,' ','') = zipData.zipcode
        AND shops.shop_name={$row['shop_name']}
    ";

    $result4 = mysql_query($query4);
    if (mysql_errno())
    { 
        die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) );
    }
    $num_rows4 = mysql_num_rows($result4);

    $num_jobs=$num_rows4;

    $i=0;
    while($row4 = mysql_fetch_array($result4))
    {
        $shopArray[$i]=$row4["shop_name"];
        $i++;
    }
    var_dump($shopArray);
}

You need to join to the shops table

SELECT shop_name FROM zipData,shops
WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+
       POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius) and
    shops.zipcode = zipdata.zipcode
select shopname from shops where zipcode in ( <your other working query> )

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