请教一个查询语句

hungels 2005-08-30 09:45:31
有三个表,分别是:hotel_base_info a,hotelhometype b,hotelhomeprice c
他们的关系是:b是a的从表(a.baseinfoid=b.baseinfoid),c是b的从表(b.homeid=c.homeid)
其中c表中有价格price。现在我需要从a,b,c中取数据,并且按照c中的价格排序,也就是说查询出来的结果,都是按照价格从低到高排序的,请问这个语句该怎么写呢?
...全文
130 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
waterfirer 2005-08-30
  • 打赏
  • 举报
回复
要用外连接吧,看你要哪个表作主表了
沝林 2005-08-30
  • 打赏
  • 举报
回复
你要怎么取数据还没说哪?如果没其它条件的话,这么写就因该是对的
select a.*,b.*
from hotel_base_info a,hotelhometype b,hotelhomeprice c
where a.baseinfoid=b.hotelbaseinfoid
and b.hotelhome_id=c.hotelhome_id
and a.citry_id='4028888f056685a201056685ba0d00e0'
order by c.sales_price
hungels 2005-08-30
  • 打赏
  • 举报
回复
好的!从a,b中取数据,取出来的结果是按照c中的价格排序
主表:hotel_base_info 字段
BASEINFOID VARCHAR2(32),
CITRY_ID VARCHAR2(32),--城市ID
NAME_CN VARCHAR2(50),--中文名称
其他省略
从表 hotelhometype
HOTELHOME_ID VARCHAR2(32) --该表主键
HOTELBASEINFOID VARCHAR2(32) --外键
HOME_NAME_CN VARCHAR2(500) --中文名称
从表 hotelhomeprice
HOMEPRICEID VARCHAR2(32)
HOTELHOME_ID VARCHAR2(32)
PIRCE FLOAT
SALES_PRICE FLOAT
CASH_PRICE FLOAT



沝林 2005-08-30
  • 打赏
  • 举报
回复
晕了,不明白,既然用了in,那里面还用order by 干嘛啊,难道in(1,2,3)和in(2,1,3)不是一样的吗。。。还是不明白你究竟要得到什么样的结果
wangzhenzhu1975 2005-08-30
  • 打赏
  • 举报
回复
select a.*
from hotel_base_info a
left join hotelhometype b on a.baseinfoid=b.hotelbaseinfoid
left join hotelhomeprice c on b.hotelhome_id=c.hotelhome_id
order by c.sales_price
hungels 2005-08-30
  • 打赏
  • 举报
回复
我尝试通过子查询来实现:
select hotel_base_info.baseinfoid from hotel_base_info where hotel_base_info.baseinfoid in (
select a.baseinfoid
from hotel_base_info a,hotelhometype b,hotelhomeprice c
where a.baseinfoid=b.hotelbaseinfoid
and a.citry_id='4028888f056685a201056685ba0d00e0'
and b.hotelhome_id=c.hotelhome_id order by c.sales_price );
但是提示:缺少右括号,应该可是使用order by的。
沝林 2005-08-30
  • 打赏
  • 举报
回复
你只说了要排序啊,其他条件都没说清楚,请把你的需求描叙完整……
hungels 2005-08-30
  • 打赏
  • 举报
回复
这样我试过了,不是我需要的结果。
select a.*
from hotel_base_info a,hotelhometype b,hotelhomeprice c
where a.baseinfoid=b.hotelbaseinfoid
and a.citry_id='4028888f056685a201056685ba0d00e0'
and b.hotelhome_id=c.hotelhome_id
order by c.sales_price
而且这样查询由于多了很多条记录
waterfirer 2005-08-30
  • 打赏
  • 举报
回复
select a.baseinfoid,b.homeid,c.price from a,b,c where a.baseinfoid=b.baseinfoid and b.homeid=c.homeid order by c.price
沝林 2005-08-30
  • 打赏
  • 举报
回复

select ... from hotel_base_info a,hotelhometype b,hotelhomeprice c
where a.baseinfoid=b.baseinfoid and b.homeid=c.homeid
order by c.price
hungels 2005-08-30
  • 打赏
  • 举报
回复
急啊!请大家帮帮忙啊
waterfirer 2005-08-30
  • 打赏
  • 举报
回复
a,b,c表如果不是一一对应,而是一对多的关系,那么很自然的在查询结果中会出现多个重复的baseinfoid,是否出现重复记录跟a,b,c表的主键选择有关。只要得到的记录是你想要的,那么排序并不是难事。
waterfirer 2005-08-30
  • 打赏
  • 举报
回复
a表和c表是否有主键
hungels 2005-08-30
  • 打赏
  • 举报
回复

目前排序如下:
select * from(
select a.baseinfoid,b.hotelhome_id,c.sales_price,c.cash_price,c.priority
from hotel_base_info a,hotelhometype b,hotelhomeprice c
where a.baseinfoid=b.hotelbaseinfoid
and b.hotelhome_id=c.hotelhome_id
and a.citry_id='4028888f056685a201056685ba0d00e0'
and ( c.cash_price<>'0' and c.cash_price is not null)
order by c.cash_price asc
) t
where (t.sales_price is not null or t.cash_price is not null)
order by t.cash_price asc
,这样排序已经没问题了,但是结果里面有很多重复的纪录!我假定上面的纪录结果集合是u,
我用 select distinct baseinfoid from u来去掉重复的纪录,可是这样,排序又乱了,请教各位该怎么解决啊?

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧