Get price from mysql between two dates - mysql

In mysql database I have table like this:
StartDate EndDate Price
01.01.2017 01.06.2017 100
02.06.2017 01.12.2017 150
What is the best solution to get price between some days. For example if I send dates 25.05.2017 as start date and 05.06.2017 as end date I need to get sum of price between these dates.

Try this
select sum(Price) as total_price from table_name
where StartDate >= 'given_startdate' and EndDate <= 'given_enddate'

To find overlapping periods use following logic:
where start_1 <= end_2 and end_1 >= start_2
in your case:
where startdate <= date '2017-06-05' and enddate >= date '2017-05-25'
Edit:
Seems like you want to expand the date range to one row per day. Join to a calendar table (if you don't have one, yer, create it, you will used it in many places):
select sum(price)
from mytable join calendar
on calendardate between startdate and enddate

Related

How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table?

I have a hotel reservation system MySQL database which has a table 'bookings'. The 'bookings' table includes two columns, datefrom (checkin) and dateto (checkout), their data-types are DATE. I need to query the database to show all bookings between two searchable dates.
The code I have is as follows:
SELECT * FROM bookings WHERE datefrom '2018-06-01' AND '2018-06-07';
This returns any booking where the start date exists between these two dates but I need to adapt the query to include bookings where the date starts before '2018-06-01' but includes the daterange shown in the query above. I understand the DATEDIFF function but I am not sure how/whether I can use that to return a date that, for example starts at '2018-05-23' and finishes '2018-06-02' (within the date range above).
I think you want this logic:
SELECT b.*
FROM bookings b
WHERE b.datefrom <= '2018-06-07' AND
b.datefrom >= '2018-06-01';
This gives you any bookings that overlap with that period.
Following logic checks for overlapping date ranges:
where start1 <= end2 and end2 >= start1
e.g. to find matching rows between '2018-06-01' and '2018-06-07'
WHERE datefrom <= '2018-06-07'
AND dateto >= '2018-06-01'
Depending on your logic you might need to switch >= to >

Using MySQL how do I select data between two dates if the dates are in separate columns

In my database, I have a column with a check-in date and a column with a check-out date. I need to select every row that has a check-in date <= 7/30/2017 and a check-out date that is >= 7/30/2017.
This is the code I have now:
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= '7/30/2017'
AND cast(checkout as date) >= '7/30/2017'
Here is an example date from the DB:
2018-09-18
When I run this query, I do not get any results, but I know that I have a check-in date equal to 7/30/2017. What am I missing? Or is there an easier way to accomplish my goal?
Assuming that you are casting valid values for date
You should convert also the literal the date properly
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= str_to_date('7/30/2017' , '%d/%m/%Y')
AND cast(checkout as date) >= str_to_date('7/30/2017' , '%d/%m/%Y')
and you can also use between
SELECT *
FROM `v_reservation_records`
WHERE str_to_date('7/30/2017','%d/%m/%Y')
between cast(checkin as date) AND cast(checkout as date)
Try like this
SELECT *
FROM `v_reservation_records`
WHERE DATE_FORMAT(checkin, '%m/%d/%Y') between '7/30/2017'
AND '7/30/2017'

MySQL Best Approach to get all days between two date values

I need to find all dates that are within two dates. This would normally be done with
BETWEEN
, but I have a StartDate and en EndDate column. This means that the date actually stretches over some days.
So a record could have values like this:
id, status, startDate, endDate
How can I find all rows, that are within those dates, provided I give the query a two dates.
To get records where the complete record period lies in the requested period:
SELECT *
FROM yourtable
WHERE startDate >= smallestDate AND
endDate <= largestDate;
To get records where the record period intersects the requested period:
SELECT *
FROM yourtable
WHERE startDate <= largestDate AND
endDate >= smallestDate;
And, for completeness, to get the records where the start of the records period lies in the requested period, but you don't care about the end of the record period:
SELECT *
FROM yourtable
WHERE startDate BETWEEN smallestDate AND largestDate
And, vice versa, for the end date of the record period:
SELECT *
FROM yourtable
WHERE endDate BETWEEN smallestDate AND largestDate
(All examples assume startDate is always smaller than the endDate, of course.)

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

mysql get current date and row date

Is it possible to do sql query on what day is today and get the row of today for date column?
So let say today is july 25th, i have database table sales, column name date, date = sales transaction date in timestamp.
i need all row that the sales date is same with current date and also is it possible set value as gmt+5?
This will get you all the rows for today's date:
SELECT * FROM sales
WHERE DATE(NOW()) = DATE(DATE_ADD(sales_transaction, INTERVAL 5 HOUR))
ORDER BY sales_transaction
As for GMT +5, Do you mean all rows with a sales date +5 hours or today +5 hours?
EDIT: Updated to add 5 hours to sales date. For a column called date, I would use the back-ticks to indicate it's a column name. e.g. SELECT `date`` FROM sales
I can't figure out how to work the back-ticks on the date field. But you should get the idea. Wrap your column names with `
Give some time and Check out Date Time Function of mySQL
SELECT * FROM tablename WHERE salesdate = '1998-1-1';
If your date is stored in GMT timezone
Select *
FROM tablename
WHERE DATE_ADD(NOW(), INTERVAL 5 HOUR) = salesdate
SELECT * FROM `sales` WHERE DAYOFMONTH(FROM_UNIXTIME(date)) = DAYOFMONTH(DATE(now()))
seems working.
Thank you for all of your replies.