Unable to fetch the accurate rooms availability in specific date range - php

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.

Related

Count How many Data in a month based on Criteria

I have 2 tables, master_kategori_project which is contain category of projects and project which is contains how many project in a specific month in a specific year. below is my master_kategori_project table.
id| category_project | description |
1 | Category 1 | descrip 1 |
2 | Category 2 | descrip 2 |
3 | Category 3 | descrip 3 |
and my project table look something like this
id| id_category_project | name_project | start_date |
1 | 1 | Project 1 | 27-01-2017 |
2 | 2 | Project 2 | 29-02-2017 |
i want to make a table that contain chart like this, u can see from the table on january i have 3 projects of kategori 1, 5 projects of kategori 2, 7 projects of kategori 3 and so on
how can i write an query that have something like this? the chart is automatically build by the table itself.
this is my model and i believe i did it wrong, because i can't put the data into the correct month
public function get_kategori_totals($mnth, $year=null)
{
$yr=date('Y');
$select = "
SELECT COUNT(category_project),start_date,category_project
FROM
project
LEFT JOIN
master_kategori_project
ON
project.id_category_project=master_kategori_project.id
WHERE
MONTH(start_date)='$mnth'".
(($year == null)?" AND YEAR(start_date)='$yr' GROUP BY category_project":"AND YEAR(stat_date)='$year' GROUP BY category_project");
return $this->db->query($select);
}
Use below query.
SELECT id_category_project, month(start_date), count(distinct name_project) as project_count
FROM project
WHERE start_date IS NOT NULL AND start_date <> '0000-00-00'
GROUP BY 1,2
ORDER BY 2,1

How do you select 3 adjacent rows with emphasis on the lower values in MySQL?

I'm trying to figure out how to select 3 adjacent rows from a table with a price < current item price. The problem is that if I'm selecting the first, second, or third row in the table, I need to select three adjacent rows around the current item. Emphasis needs to be put on the lower price items, for example, if I'm selecting the third row from the table, I need to select the first two rows and the fourth row. Here is my query so far:
SELECT *
FROM (SELECT *
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND price < '$price'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
Sample data:
+---------------+---------+--------+-------+
| hotel_name | airport | people | price |
+---------------+---------+--------+-------+
| Days Inn | MLB | 1 | 109 |
| Holiday Inn | MCO | 2 | 149 |
| Americas Best | MLB | 2 | 199 |
| Econo Lodge | SFB | 1 | 209 |
+---------------+---------+--------+-------+
Expected results:
Selected hotel: Americas Best
+---------------+-------+---------+--------+-------+
| hotel_name | order | airport | people | price |
+---------------+-------+---------+--------+-------+
| Days Inn | 1 | .. | .. | .. |
| Holiday Inn | 2 | .. | .. | .. |
| Americas Best | Skip | .. | .. | .. |
| Econo Lodge | 3 | .. | .. | .. |
+---------------+-------+---------+--------+-------+
PHP/MySQL combination can be used for an answer. Any help would be appreciated.
You can union the two records with lower prices with the record of higher price:
select * from
(
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price < '245'
ORDER BY price DESC LIMIT 3)
UNION
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price > '245'
ORDER BY price LIMIT 1)
)
order by price desc limit 3
In the first query, you select 3 rows and in the second, 1 row (if it exists). Finally, Out of these 3+1 (or 3+0) rows, you select only 3 rows with highest price.
SELECT *
FROM (SELECT MIN(price)
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND hotel != '$hotel'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
After 2 hours, I finally found the solution. I combined a union with two select queries that selects 3 rows before (and including the selected hotel) and 3 rows after the selected hotel. I then set limit 3 and order by price asc and everything it mostly works.
SELECT *
FROM (
(
SELECT *
FROM temp_db_cart
WHERE price >= {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price ASC limit 3 )
UNION
(
SELECT *
FROM temp_db_cart
WHERE price < {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price DESC limit 3 ) ) AS u
WHERE hotel_name != '{$package['hotel_name']}'
ORDER BY price ASC limit 3;
Edit: The limiting of 3 rows in the parent select query only limits to the first 3 rows from the union set. How do I limit with an offset starting at 2 rows before the selected hotel and ending at 2 rows after the selected hotel of the union subset?

How to get the latest data of a specific day? [MYSQL]

How can I get the latest data of a specific day in MySQL?
Let's assume that I have a column of dates recorded on my database
dates | time | value
---------------------------------------------
2015-08-05 | 11:03:02 | 200
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:00:11 | 400
2015-08-08 | 13:23:11 | 200
2015-08-09 | 17:00:23 | 2200
2015-08-09 | 17:06:00 | 1290
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
I want to get the latest data or transaction of a specific date, my desired result would be like this
dates | time | value
---------------------------------------------
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:23:11 | 200
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
Only the latest data of a spefic or distinct date is chosen, what is the possible query with this?
You need to do this way to get that for each dates
select t1.dates,t1.time,t1.value from table as t1 inner join
(
select dates,max(time) as time from table group by dates
) as t2 on t1.dates=t2.dates and t1.time=t2.time
Use
SELECT * FROM TABLE_NAME GROUP BY dates ORDER BY time desc LIMIT 1;
Or
SELECT * FROM TABLE_NAME GROUP BY dates HAVING time <= '23:59:59' LIMIT 1;
Try this:
SELECT `dates`, MAX(`time`) AS `time`, MAX(`value`) AS `value`
FROM `tbl_name`
GROUP BY `dates`
ORDER BY `dates` ASC
Try this query:
SELECT * FROM `table` GROUP BY dates ORDER BY time DESC;

Calculate price between different dates

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 |
+---------+------+-----------+

how to select all within a category if at least one falls within range

I'm using MySQL inside PHP.
I have an SQL table that looks something like this:
id | category_id | date
-----------------------------
1 | 3 | 2012-09-12
2 | 4 | 2012-10-25
3 | 3 | 2012-10-12
4 | 3 | 2012-10-02
5 | 4 | 2012-11-03
6 | 3 | 2012-11-02
I'm trying to figure out how can select all dates of the given category if at least one falls within the specified date range, otherwise select none.
For example:
If the date range is from 2012-09-01 to 2012-09-31, then the query should return all rows for category 3 and none for category 4.
Is there a way do this in a single query ?
Thanks!
You do this with a join or in clause:
select t.*
from t join
(select distinct category_id
from t
where date between <datefrom> and <dateto>
) tc
on t.category_id = tc.category_id
order by t.category_id, date

Categories