使用order字段从mysql中选择查询

时间:2015-12-01 02:48:06

标签: php mysql codeigniter

我有如下的mysql查询

SELECT
p.prop_name,
p.prop_id,
p.star,
c. NAME,
pr.prov_name,
c.countryid,
c. NAME,
pr.provinceid,
r.room_typeid,
r.roomtype_name,
rr.is_promo,
r.price,
rr.final_price FROM
v_property p 
INNER JOIN set_country c ON(p.countryid = c.countryid)
INNER JOIN set_province pr ON(p.provinceid = pr.provinceid)
INNER JOIN room_type r ON(p.prop_id = r.prop_id)
INNER JOIN room_rate_promo rr ON(r.room_typeid = rr.room_typeid)
WHERE
  rr.date = '2015-11-09'
AND p.is_active = 1
AND rr.rating_status = 'ON'
AND p. STATUS = 1
AND p.provinceid = '5'
ORDER BY
  rr.final_price ASC

,结果显示为低于

enter image description here

我希望prop_name只显示每个prop_name的一行,并按最小final_price列获取并按is_promo排序

2 个答案:

答案 0 :(得分:0)

你快到了。只需在“SELECT”后添加“TOP 1”即可。所以它会是:

SELECT TOP 1
/* etc... */

答案 1 :(得分:0)

使用 GROUP BY 子句使prop_name唯一。

SELECT
    p.prop_name,
    p.prop_id,
    p.star,
    c. NAME,
    pr.prov_name,
    c.countryid,
    c. NAME,
    pr.provinceid,
    r.room_typeid,
    r.roomtype_name,
    rr.is_promo,
    r.price,
    rr.final_price FROM
    v_property p 
    INNER JOIN set_country c ON(p.countryid = c.countryid)
    INNER JOIN set_province pr ON(p.provinceid = pr.provinceid)
    INNER JOIN room_type r ON(p.prop_id = r.prop_id)
    INNER JOIN room_rate_promo rr ON(r.room_typeid = rr.room_typeid)
    WHERE
      rr.date = '2015-11-09'
    AND p.is_active = 1
    AND rr.rating_status = 'ON'
    AND p. STATUS = 1
    AND p.provinceid = '5' 
    GROUP BY p.prop_name 
    ORDER BY
      rr.final_price ASC, is_promo DESC