大家好我是新手,我需要有关预订酒店的帮助,我有这张桌子:
id, idhotel, room, data_start, data_end, price
现在我需要计算同一天在多天之间搜索房间时的总价 我正在尝试此查询,但结果为0。
$query = "SELECT room, SUM(price) FROM price WHERE idhotel='".$_GET['id']."' BETWEEN 'data_start' AND 'data_end'
ORDER BY room";
有人可以帮助我吗?
答案 0 :(得分:0)
我认为你需要这样的东西:
SELECT idhotel, room, price, (DATEDIFF(data_end, data_start) * price) as total FROM rooms
为了测试创建下一个表并填写它,请选择*:
create table rooms (id int not null primary key auto_increment, idhotel int, room int, data_start date, data_end date, price int);
+----+---------+------+------------+------------+-------+
| id | idhotel | room | data_start | data_end | price |
+----+---------+------+------------+------------+-------+
| 1 | 1 | 1 | 2013-05-09 | 2013-05-12 | 100 |
| 2 | 1 | 1 | 2013-05-20 | 2013-05-20 | 100 |
+----+---------+------+------------+------------+-------+
使用下一个查询获取总费用(修改以防止丢失一天):
SELECT idhotel, room, price, ((DATEDIFF(data_end, data_start) + 1) * price) as total FROM rooms;
结果:
+---------+------+-------+-------+
| idhotel | room | price | total |
+---------+------+-------+-------+
| 1 | 1 | 100 | 400 |
| 1 | 1 | 100 | 100 |
+---------+------+-------+-------+
如果您想获得期间的房间总和,请尝试使用以下代码:
SELECT
idhotel, room, SUM(total) as total_sum
FROM
(
SELECT
idhotel,
room,
price,
(DATEDIFF(data_end, data_start) * price) as total
FROM
rooms
) as t
GROUP BY
idhotel, room;
结果:
+---------+------+-----------+
| idhotel | room | total_sum |
+---------+------+-----------+
| 1 | 1 | 150 |
+---------+------+-----------+
答案 1 :(得分:0)
如果我理解正确你可能正在寻找类似的东西
$date_start = $_GET['date_start'];
$date_end = $_GET['date_end'];
$hotel_id = $_GET['id'];
$query = "SELECT room,
SUM(price) total
FROM tablename
WHERE data_start >= '$date_start'
AND date_end <= '$date_end'
AND idhotel = $hotel_id
GROUP BY room";