简体   繁体   English

使用需要放入数组的字段搜索查询

[英]searching a query with fields that need putting into array

not sure if I am attempting this correct but I have a MySQL database that stores Latitude, Longitude and vehicles along with other fields. 不知道我是否尝试正确,但是我有一个MySQL数据库,该数据库存储纬度,经度和车辆以及其他字段。 The issue I am having is that I am storing the vehicles in the database like '1:4:7:8:9' etc. 1 being Car but that's not important. 我遇到的问题是我将车辆存储在数据库中,例如“ 1:4:7:8:9”,等等。1是Car,但这并不重要。 The code I have works getting the result based on the long,lat but I need it to get results if the vehicles type is also a match, so I need to add something to the query to see if either 1 or whatever is in that field that contacts '1:4:7:8:9' 我使用的代码可以根据经度和纬度来获取结果,但是如果车辆类型也匹配,我需要它来获取结果,因此我需要向查询中添加一些内容,以查看该字段中是否包含1或任何内容与“ 1:4:7:8:9”联系

The code is 该代码是

    include 'db.php';

$origlat = "53.83428999999999";
$origlon = "-3.0399797000000035";
$vehicle_req = "1";

$result = mysqli_query($con,"SELECT *, ( 3959 * acos( cos( radians(53.83428999999999) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-3.0399797000000035) ) + sin( radians(53.83428999999999) ) * sin( radians( latitude ) ) ) ) AS distance FROM clients HAVING distance < 30 ORDER BY distance LIMIT 0 , 20;");

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

$id = $row['id'];

echo "found";
echo $id;

Thankyou and I hope this is clear? 谢谢,我希望这很清楚? Thanks, Zak 谢谢,扎克

If you want search 1 and 4 in id column: 如果要在id列中搜索1和4:

/* MySQL Query Should be:
SELECT *, (...) AS distance FROM clients
HAVING distance < 30 
  AND INSTR( concat('-:',id,':') , ':1:') > 0
  AND INSTR( concat('-:',id,':') , ':4:') > 0
ORDER BY distance LIMIT 0 , 20;
*/

to create this MySQL Query we need for loop for each number in $vehicle_reqs 要创建此MySQL查询,我们需要for $vehicle_reqs每个数字for循环

$vehicle_reqs= array(1,4);
// fix part of MySQL Query
$sqlQuery = "SELECT *, ( 3959 * acos( cos( radians(53.83428999999999) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-3.0399797000000035) ) + sin( radians(53.83428999999999) ) * sin( radians( latitude ) ) ) ) AS distance FROM clients HAVING distance < 30 "

// conditions for all items in $vehicle_reqs array:
for( $i=0; $i<sizeof($vehicle_reqs); $i++){
    $sqlQuery = $sqlQuery . " AND INSTR( concat('-:',id,':') , ':".$vehicle_reqs[i].":') > 0 " ;
}

// another fix part of MySQL Query
$sqlQuery = $sqlQuery ." ORDER BY distance LIMIT 0 , 20;" ;
echo $sqlQuery ;

How INSTR( concat('-:',id,':') , ':4:') > 0 works ? INSTR( concat('-:',id,':') , ':4:') > 0工作?

1- adding : to start and end of id column in memory. 1-添加:在内存中id列的开始和结束。

2- finding first substring that is equal to ':4:' 2-查找等于':4:'的第一个子字符串

3- if INSTR() not find any thing, retuns 0 3-如果INSTR()找不到任何东西,则重新调整0

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

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