2

我在编写查询时遇到问题,我有两个表。

  1. 属性表(属性)

    • ID
    • property_name
    • 财产种类
    • 价格

有两种类型的财产,商业或住宅。如果类型是住宅,则价格是整个物业的价格。但是如果是商业类型,每个公寓的价格都不一样,比如一楼,一楼,二楼,三楼。等等。所以我做了第二张桌子

  1. 物业价格表 (price_for_commerical_prop),包含以下字段

    • ID
    • property_id
    • floor_id
    • 价格

此表可能包含一个属性的多行,具体取决于该属性的单位数量。floor_id 包含值 0 表示一楼,1 表示一楼,依此类推。

我的问题是这样的。我有一个搜索字段来根据价格搜索房产。所以如果用户输入价格,我必须在第一张表和第二张表的价格字段中搜索价格。并根据搜索条件返回行。任何人都可以阐明如何做到这一点?

4

1 回答 1

0

要搜索 10 到 20 的价格范围,您可以:

select  *
from    Property p
left join
        price_for_commerical_prop pfcp
on      p.id = pfcp.property_id
where   case 
        when p.property_type = 'residential' then p.price
        when p.property_type = 'commercial' then pfcp.price
        end between 10 and 20

case返回Property住宅价格和商业价格price_for_commerical_prop

于 2012-09-20T18:26:22.157 回答