简体   繁体   English

需要帮助来创建一个mysql查询

[英]need help creating a mysql query

Let's assume I a have a table called "users" which has the following relevant columns: 假设我有一个名为“ users”的表,该表具有以下相关列:

  • username 用户名
  • zipcode 邮政编码

Let's also assume I have a table called "shops" which has the following relevant columns: 还假设我有一个名为“ shops”的表,该表具有以下相关列:

  • shop_name shop_name
  • zipcode 邮政编码

Let's also assume I have a table called "preferred_shops" which has the following relevant columns: 还假设我有一个名为“ preferred_shops”的表,该表具有以下相关列:

  • shop_name shop_name
  • username 用户名

Let's also assume I have a table called "zipData" which has the following relevant columns: 还假设我有一个名为“ zipData”的表,该表具有以下相关列:

  • 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 从zipData表中找到用户的经/纬度
  • Find all the shops within a given radius that is also in a "preferred_shops" table with the given user in the "username" column 在“用户名”列中使用给定用户在给定半径内(也在“ preferred_shops”表中)找到所有商店

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 需要考虑的问题:出于美观原因,“ zipData”中的邮政编码没有空格,但“用户”和“商店”中的邮政编码却具有空格...例如加拿大邮政编码,格式为“ A1A 1A1”

==== 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> )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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