我有如下的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
我希望prop_name
只显示每个prop_name
的一行,并按最小final_price
列获取并按is_promo
排序
答案 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