简体   繁体   English

排除在搜索结果中首先获取的结果

[英]exclude results which are fetched first in search result

I'm working on a property project where user searches for properties in some locality. 我正在开发一个属性项目,用户在某个地方搜索属性
After fetching the relevant results I would like to display some suggestive results which should not contain the results previous displayed. 取得相关结果后,我想显示一些建议性结果,其中不应包含先前显示的结果。

Table schema is: 表模式是:

property table:

+-----+-------------+-----------+-------------+------+----------+
| id  | property    | amenities | description | city | locality |
+-----+-------------+-----------+-------------+------+----------+
| 1   | House/Villa |           | some desc   | cit1 | loc1     |
+-----+-------------+-----------+-------------+------+----------+
| 2   | Office      |   ....    | desc....    | cit1 | loc2     |
+-----+-------------+-----------+-------------+------+----------+
| 3   | House/Villa | .....     | desc....    | cit1 | loc3     |
+-----+-------------+-----------+-------------+------+----------+

I want, if the user searches for House/Villa in city city1 and locality loc1 then along with the available results I would like to suggest him for other properties in same city with other locality . 我想要的是,如果用户在城市 city1位置 loc1搜索House/Villa ,然后连同可用结果一起向他建议与其他位置在同一城市的其他物业。
What would be the SQL query. 什么是SQL查询。 I've to use it in PHP 我必须在PHP使用它

let your first query is 让你的第一个查询是

select * from property where city="city1" and locality="loc1" and property ="House/Villa";

then your query for desired result should be 那么您对期望结果的查询应该是

select * from property where  property ="House/Villa" and city="city1" and locality NOT IN(loc1);

In will be better if you are showing him result for multiple location in first time, and want to omit that then you can pass an array by makin an comma separated string, 如果您是第一次向他显示多个位置的结果,并且想省略,那么可以使用逗号分隔的字符串来传递数组,这样会更好

$notIn = implode(", ", array("loc1","loc2"));

so NOT IN will fetch result that would not include passed location. 所以NOT IN将获取不包含传递位置的结果。

Step to optimize this query. 步骤以优化此查询。

  1. select only required column, do not use * 只选择所需的列,不要使用*

  2. use limit eg:- select * from property where property ="House/Villa" and city="city1" and locality NOT IN(loc1) limit 0,30; 使用限制,例如:- select * from property where property ="House/Villa" and city="city1" and locality NOT IN(loc1) limit 0,30; // this will fetch only top 30 result. //这将仅获取前30个结果。 you can set according to your way. 你可以根据自己的方式设置。

  3. select filtered data by using proper queries and subqueries 使用适当的查询和子查询选择过滤的数据

  4. Not In is slow for large result b/c mysql first scan full table then apply Not IN, so you can go for NOT EXISTS() instead of NOT IN(). 对于大量结果,Not In很慢,b / c mysql首先扫描整个表,然后应用Not IN,因此可以使用NOT EXISTS()而不是NOT IN()。

Try this : 尝试这个 :

For proper answer: 正确答案:

SELECT DISTINCT property, amenities, description, city, locality FROM property
WHERE property = 'House/Villa' AND city = 'city1' AND locality = 'loc1'

For suggestions use: 对于建议使用:

 SELECT DISTINCT property, amenities, description, city, locality FROM property
    WHERE property = 'House/Villa' AND city = 'city1' AND locality != 'loc1'
$property = 'House/Villa';
$city = 'city1';
$locality = 'loc1';

$q = "SELECT * FROM property WHERE property = '" . $property . "'AND city = '" . $city . "'";

$stmt = pdoObject->prepare($q);
$stmt->execute() or die(print_r($stmt->errorInfo()));
$result = $stmt->fetchAll(PDO::FETCH_BOTH);
foreach($resukt as $res)
{
   if($res['locality'] == $locality)
    echo "your search result";
} 

foreach($resukt as $res)
{
   if($res['locality'] != $locality)
    echo "this is suggestion";
}

I assume you are using PDO. 我假设您正在使用PDO。

You should be able to do this with just one SQL query 您应该只用一个SQL查询就能做到这一点

$query = "SELECT *, IF(locality='" . mysqli_real_escape_string($locality) . "', 1, 0) as locality_match FROM property WHERE property = '" . mysqli_real_escape_string($property) . "'AND city = '" . mysqli_real_escape_string($city) . "' ORDER BY locality_match DESC";

This puts the locality if statement in an if statement as part of the select. 这会将局部if语句放在if语句中,作为select的一部分。 So it doesn't limit the results but instead gives us a value to sort by, so the items that match the locality will be first and suggestions will be at the end. 因此,它不会限制结果,而是为我们提供排序的值,因此匹配本地的项目将是第一个,建议将在最后。

I also added an escape function around each parameter, because you definitely want to sanitize any input from the user. 我还在每个参数周围添加了一个转义函数,因为你肯定想要清理用户的任何输入。

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

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