Calculate price between different dates - php

Hi everyone I'm new and I need help about my booking hotel, I've this table:
id, idhotel, room, data_start, data_end, price
Now I need to calculate the total price when samebody search the rooms between many days
I'm trying this query but the result is 0.
$query = "SELECT room, SUM(price) FROM price WHERE idhotel='".$_GET['id']."' BETWEEN 'data_start' AND 'data_end'
ORDER BY room";
Can someone one help me?

If I understand correctly you are probably looking for something like this
$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";

I think you need something like this:
SELECT idhotel, room, price, (DATEDIFF(data_end, data_start) * price) as total FROM rooms
For testing create next table and fill itselect * :
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 |
+----+---------+------+------------+------------+-------+
Use next query for get total cost (modifed for prevent losing one day):
SELECT idhotel, room, price, ((DATEDIFF(data_end, data_start) + 1) * price) as total FROM rooms;
Result:
+---------+------+-------+-------+
| idhotel | room | price | total |
+---------+------+-------+-------+
| 1 | 1 | 100 | 400 |
| 1 | 1 | 100 | 100 |
+---------+------+-------+-------+
Extend
If you want get room total sum for period, try use this code:
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;
Result:
+---------+------+-----------+
| idhotel | room | total_sum |
+---------+------+-----------+
| 1 | 1 | 150 |
+---------+------+-----------+

Related

How to select only available time and date for doctor appointment booking system in PHP

I have only one table named appointment_master in MySQL in which I store the booking date and time entered by the patient. Appointment time is fixed means patient can book an appointment every 10 minutes means if one patient has booked appointment at 10:00 AM then other can book at 10:10 AM and so on. Now if an appointment is booked at 10:00 AM and when other patient try to book an appointment the date and time slot option 10:00 AM should not be displayed to other patient in the form select option tag.
My table is as follows:
name lastname age gender email phone_no app_date app_time
ramesh ahir 30 Male rames#gmail.com 9824758745 2019-01-18 10:20:00
jitesh thacker 35 Male jitesh#gmail.com 9824758745 2019-01-21 10:30:00
Consider the following, very simplified example:
DROP TABLE IF EXISTS slots;
CREATE TABLE slots
(id SERIAL PRIMARY KEY,slot VARCHAR(12) NOT NULL UNIQUE);;
INSERT INTO slots VALUES
(1,'Slot 1'),
(2,'Slot 2'),
(3,'Slot 3'),
(4,'Slot 4'),
(5,'Slot 5');
DROP TABLE IF EXISTS bookings;
CREATE TABLE bookings
(booking_id SERIAL PRIMARY KEY
,user_id INT NOT NULL
,slot_id INT NOT NULL UNIQUE
);
INSERT INTO bookings VALUES
(1,101,3);
To show available slots, we can do something like this...
SELECT s.*
FROM slots s
LEFT
JOIN bookings b
ON b.slot_id = s.id
WHERE b.booking_id IS NULL;
+----+--------+
| id | slot |
+----+--------+
| 1 | Slot 1 |
| 2 | Slot 2 |
| 4 | Slot 4 |
| 5 | Slot 5 |
+----+--------+
...or this...
SELECT s.*
, CASE WHEN b.booking_id IS NULL THEN 'yes' ELSE 'no' END available
FROM slots s
LEFT
JOIN bookings b
ON b.slot_id = s.id;
+----+--------+-----------+
| id | slot | available |
+----+--------+-----------+
| 1 | Slot 1 | yes |
| 2 | Slot 2 | yes |
| 3 | Slot 3 | no |
| 4 | Slot 4 | yes |
| 5 | Slot 5 | yes |
+----+--------+-----------+
To make sure two users can't book the same slot simultaneously, we can do something like this...
INSERT INTO bookings (user_id,slot_id)
SELECT 102,1
FROM (SELECT 1) x
LEFT
JOIN bookings y
ON y.slot_id = 1
WHERE y.booking_id IS NULL;
SELECT * FROM bookings;
+------------+---------+---------+
| booking_id | user_id | slot_id |
+------------+---------+---------+
| 1 | 101 | 3 |
| 2 | 102 | 1 |
+------------+---------+---------+
...which prevents another user booking the same slot...
INSERT INTO bookings (user_id,slot_id)
SELECT 103,1
FROM (SELECT 1) x
LEFT
JOIN bookings y
ON y.slot_id = 1
WHERE y.booking_id IS NULL;
SELECT * FROM bookings;
+------------+---------+---------+
| booking_id | user_id | slot_id |
+------------+---------+---------+
| 1 | 101 | 3 |
| 2 | 102 | 1 |
+------------+---------+---------+
Everything else can be handled in your application code (PHP).
use the below query to get the last appointment time
select max(app_time) from appointment_master;
Save this time in a PHP variable $date1 (or any name)
Save the new appointment time as $date2 variable
get the difference between the two variables
Use if condition to prevent the appointment if the time difference is less than 10 minutes

php mysql select by month between records

I've this MySQL table my_table:
+-------+------------+-----------+
|Student| Date | Classroom |
+-------+------------+-----------+
| 1 | 2018-01-01 | 101 |
| 2 | 2018-01-01 | 102 |
| 3 | 2018-01-01 | 103 |
| 1 | 2018-03-01 | 104 |
| 2 | 2018-06-01 | 103 |
| 3 | 2018-09-01 | 104 |
| 1 | 2018-11-01 | 106 |
| 2 | 2018-12-01 | 101 |
+-------+------------+-----------+
The students stay in the assigned classroom till changed.
I'm trying to get which classroom they were in for a certain month.
For example in October(10), student 1 was in 104, 2 was in 103, and 3 was in 104.
I'm really unsure on how to proceed with this one so any help is appreciated.
Currently using this query based on Strawberry answer
SELECT x.*
FROM my_table x
LEFT OUTER JOIN my_table y
ON y.student = x.student
AND y.date < x.date
WHERE x.date <= LAST_DAY('2018-10-01')
GROUP BY student
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(Student INT NOT NULL, Date DATE NOT NULL, Classroom INT NOT NULL,PRIMARY KEY(student,classroom));
INSERT INTO my_table VALUES
(1,'2018-01-01',101),
(2,'2018-01-01',102),
(3,'2018-01-01',103),
(1,'2018-03-01',104),
(2,'2018-06-01',103),
(3,'2018-09-01',104),
(1,'2018-11-01',106),
(2,'2018-12-01',101);
SELECT x.*
FROM my_table x
JOIN
( SELECT student
, MAX(date) date
FROM my_table
WHERE date <= LAST_DAY('2018-10-01')
GROUP
BY student
) y
ON y.student = x.student
AND y.date = x.date;
+---------+------------+-----------+
| Student | Date | Classroom |
+---------+------------+-----------+
| 1 | 2018-03-01 | 104 |
| 2 | 2018-06-01 | 103 |
| 3 | 2018-09-01 | 104 |
+---------+------------+-----------+
Here's a go at it (snippet to go in a stored procedure; assumes table called example & output to table months). It produces a row per student for each month of the range.
drop table months;
create table months (month date, student integer, classroom integer);
set #month = (select min(date) from example);
start_loop: LOOP
insert into months select #month, s1.student, classroom from
(select student, max(date) as maxdate from example where date <= #month group by student) s1
join example s2 on s1.student = s2.student and maxdate = date;
if #month = (select max(date) from example) then
leave start_loop;
end if;
set #month = #month + interval 1 month;
END LOOP start_loop;
Let's break the problem into two parts. Firstly, find all the rooms which have been allocated to student A so far and sort them using the date. Next, find the record which is just before or equal to the required month.
For example:
Consider student 1. We get
+-------+------------+-----------+
|Student| Date | Classroom |
+-------+------------+-----------+
| 1 | 2018-01-01 | 101 |
| 1 | 2018-03-01 | 104 |
| 1 | 2018-11-01 | 106 |
+-------+------------+-----------+
Now, let's say for month June we try to find month just less than or equal to 2018-06-01 to get the required room number. I hope this will help.

How to calculate grand total in mysql without group by clause in mysql

i want to get grand total of a column without using group by clause as i don't want to concat any string.
i looking for this kind desired result:
| customer id | project name | Product Name | Quantity | price | total |
+--------------+--------------+--------------+----------+--------+----------+
| 9 | xyz | ppn | 2 | 2 | 4 |
+--------------+--------------+--------------+----------+--------+----------+
| 11 | pqr | xxx | 2 | 2 | 4 |
+--------------+--------------+--------------+----------+--------+----------+
| | | | | | 8 |<=== Grand total
+---------------------------------------------------------------------------+
here is SQL Fiddle:http://sqlfiddle.com/#!9/30fdd5/2
I think this does what you want:
SELECT customer_id, project_name, product_name, quantity,
price, quantity * price as total
FROM project_expenses_data
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, sum(quantity * price)
FROM project_expenses_data;
To be 100% clear that the total is the last row in the result set, add the clause ORDER BY (customer_id IS NULL) ASC.

Mysql Group By and Sum Total of the column values

enter image description here I am having one table like id, sale_id, item_total, tax fields. need to sum the item_total by grouping the tax values.
Table 1
id | sale_id | item_cost_price | tax |
1 | 10 | 150 | 5 |
2 | 10 | 50 | 7 |
3 | 10 | 30 | 5 |
this is required output:
id | sale_id | item_cost_price | tax |
1 | 10 | 180 | 5 |
2 | 10 | 50 | 7 |
When i tried this query,
SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax
$query=$this->db->query("SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax ");
echo $num = $query->num_rows();
$result=array();
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->item_cost_price;
}
My output is:
i am getting output like this,
am getting distinct tax only. but i need to sum item total values.
Note:
Image 1 : refer my datatable
Image 2: refer my expected outputenter image description here
Add SUM(item_cost_price) in SELECT statement.
In your select statement your select only sale_id, tax. But what you echo are sale_id, tax, and item_cost_price which not exist in your SELECT statement. Try This:-
$sql = "SELECT sale_id,tax, SUM(item_cost_price ) AS TotalPrice FROM bgs_ib_sales_items WHERE sale_id = '10' GROUP BY tax";
$query=$this->db->query($sql);
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->TotalPrice;
}

Unable to fetch the accurate rooms availability in specific date range

Here is my database structure
Table name :set_inventory, and columns are below
inventory_id | room_id | quantity_start_date | quantity_end_date | total_rooms
1 | 2 | 2015-10-10 | 2015-10-12 | 5
2 | 2 | 2015-10-13 | 2015-10-14 | 10
3 | 2 | 2015-10-15 | 2015-10-17 | 0
Another Table
Table name : rooms, amd columns are
room_id | room_type | room_picture | room_description
2 | standard | pic_link | demo description
Description: In inventory table admin able to set the inventory based on multiple date ranges.
My query:
SELECT rooms.room_id, rooms.room_pic1, rooms.room_type, rooms.maximum_adults,
rooms.maximum_children, rooms.room_amenities,set_inventory.room_id,
set_inventory.quantity_start_date, set_inventory.quantity_start_date,
set_inventory.total_rooms
from rooms, set_inventory
WHERE rooms.room_id = set_inventory.room_id
AND quantity_start_date <= '2015-10-11'
AND quantity_end_date > '2015-10-13'
It shows me the inventory only 5, based in above query,
What i am looking for :actually, i am looking for the result,
for example my check_in_date "2015-10-11" and check_out_date is "2015-10-17"
In this i will be able to notify the customer that
"room is only available for 4 days,
Plz help me , thanks
UPDATED
Try this WHERE condition:
WHERE rooms.room_id = set_inventory.room_id AND DATE(quantity_start_date) <= '2015-10-11' AND DATE(quantity_end_date) > '2015-10-13';
So, minding you want to have the amount of days between these to dates, your query should contain somwthing like this:
SELECT ABS(DATEDIFF(a1.start_date, a2.end_date)) FROM (
SELECT quantity_start_date as start_date FROM set_inventory WHERE quantity_start_date <= '2015-10-11' ORDER BY start_date DESC LIMIT 1) a1, (
SELECT quantity_end_date as end_date FROM set_inventory WHERE quantity_end_date > '2015-10-13' ORDER BY end_date ASC LIMIT 1) a2;
If you try it, you'll get 4 in result.

Categories