【问题标题】:sorting search results in custom criteria按自定义条件对搜索结果进行排序
【发布时间】:2012-10-11 13:08:35
【问题描述】:

我会尽量解释我的问题。

我有一个搜索表单,用户可以在其中选择不同的参数并输入不同的值来执行属性搜索。就像我有一张桌子tbl_properties

在搜索表单中,用户选择property_categoryproperty_type[租赁、出售或出租],输入价格范围no_of_bedroomslocationdistrictsproperty_area等。可能在tbl_properties 或其他查找表中。写查询不是我的问题,但我遇到的问题是,我想在搜索结果中按照以下模式对记录进行排序

  1. 首次显示结果 - 与位置匹配的价格以降序排列
  2. 然后显示 - 与该地区匹配的价格
  3. 然后显示 - 同一区的卧室数量匹配
  4. 然后显示 - 价格/卧室/房产面积匹配但在不同的区域

我只是想提示如何按这些顺序对记录进行排序?

编辑

这里是我的表结构的简要说明

tbl_properties
-------------------

property_id   INT
category_id    INT
property_name VARCHAR
price         INT
district_id   INT
location       VARCHAR
property_type  ENUM('lease','sale','rent')


tbl_category
-------------
category_id   INT
category_name VARCHAR


tbl_districts
-----------------
district_id INT
district_name VARCHAR


tbl_property_details
------------------------
detail_id      INT
property_id     INT
no_of_bedrooms  INT
property_area  DECIMAL

谢谢

【问题讨论】:

  • 您确定您的意思是按价格降序排列吗?比如,最高价优先?
  • @RedFilter,请看我的编辑

标签: mysql sql


【解决方案1】:
order by case
    when price between @priceMin and @priceMax 
        and location = @location then 1
    when price between @priceMin and @priceMax 
        and district_id = @districtid then 2
    when no_of_bedrooms = @no_of_bedrooms 
        and districtid = @districtid then 3
    when price between @priceMin and @priceMax 
        and no_of_bedrooms = @no_of_bedrooms 
        and property_area = @property_area and districtid <> @districtid then 4
    else 5
end, price desc

【讨论】:

    【解决方案2】:

    从 tbl_properties 中选择 property_category property_type _ __ _ _ _ _ 排序方式 ___;

    所以...按价格顺序排序;

    等等

    这是使用http://www.sqlcourse2.com/orderby.html的好链接

    【讨论】:

    • 这不是我想要的,请仔细阅读我的问题。
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多