我有一个公寓日历表,用于显示每天用户的价格。 在这张桌子上,部分公寓包括出租房间(id_room):
表格列/记录示例:
id_apart | id_room | date | price | promo_price
1 | 1 | 03-03-2013 | 20.00 | 0
1 | 1 | 04-03-2013 | 20.00 | 0
1 | 2 | 03-03-2013 | 50.00 | 45.00
1 | 2 | 04-03-2013 | 50.00 | 45.00
我希望得到一个字符串,其中'price'和'promo_price'与两个日期之间公寓/房间的最低价格总和相连。
此查询总结了公寓的所有房价,我无法弄清楚如何在这个房间使用MIN,只计算CHEAPEST房间的价格:
select
concat(sum(if(promo_price>0,promo_price,price)),
"---",
sum(price))
from
apart
where
id_apart=215
and date>= "2013-03-03"
and date<"2013-03-05"
此查询的结果是: 90 --- 140
图例:第一个字符串数字是'promo_price'的总和; “价格”的第二个数字总和
答案 0 :(得分:0)
select concat(promo_price, "--", price) from apart
where id_apart=215 and date >= "2013-03-03" and date < "2013-03-05"
and (promo_price + price)) = (select min(promo_price + price) from apart);
答案 1 :(得分:0)
请试试这个
select concat(if(promo_price>0, 'true', sum(promo_price)),'---', sum(price))
from aprt where id_apart = 215 and date between "2013-03-03" and "2013-03-05"
答案 2 :(得分:0)
我认为这就是你要找的东西
select if(promo_price = 0,concat(sum(price),"---", sum(price)) ,sum(price)) as pri
from apart
where id_apart=1 and `date`>= "03-03-2013" and `date` < "05-03-2013"
and promo_price = 0
OUTPUT。
PRIS
<强> 40 --- 40 强>
obs 请注意,我从上面的例子中得到了id_apart = 1。
答案 3 :(得分:0)
这是你要找的答案吗?
select
concat(sum(if(promo_price>0,promo_price,price)),
"---",
sum(price)) from apart group by id_room
having sum(price) + sum(promo_price) =
(select min(sum(price) + sum(promo_price)) from apart where id_apart =1
group by id_room);
输出:40 --- 40