Get data between start date and end date
I am getting data but my requirement is little bit different pls check the below database
I have two text boxs
1) start date
2) end date
Database is like this
table name->purchase_order
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
5 2017/10/01 2017/11/30
I am trying like this
select *
from purchase_order
where start_date>= '2017/09/01' and start_date<= '2017/09/01'
OR end_date>= '2017/09/01' and end_date<= '2017/09/01'
Output i am getting
sno start_date end_date
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
What i require
if i select between this 2017/09/01 - 2017/09/30 i want out put like this {in id "1" in between 8th month to 12th month there is 9th month is there so it also has to come}
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
thanks
Instead of checking that the column values are between your dates, you want to see if your dates are between the column values. Using BETWEEN makes the logic look a little cleaner:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date
If you select 2017/09/01 - 2017/09/30, then do this:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date AND '2017/09/30' BETWEEN start_date AND end_date
AND will make sure both dates are between start_date and end_date, OR will make sure at least one of the dates is between the columns.
You would not want to have 4 clauses as you have in your question, you would want to have just 2.
The logic should be as follows:
the start date from the input in the application is later or equal to the start date in the start column in your table
the end date from the input in the application is earlier or equal to the end date in the end column in your
application.
Based on this logic, you should be able to use the following query:
SELECT
*
FROM purchase_order
WHERE start_date<= '2017/09/01' -- start date input
AND end_date<= '2017/09/01' -- end date input
One may complain that I have a hole in my logic due to the fact that I don't check to see that the end date is later than the start date, though you would want to do that at the application level, hence it is not relevant at the database level where this question was asked.
Related
I'm trying to grab SQL data via PHP with a tally for case types each week to display like so:
Week 1 | Date From | Volume
Week 2 | Date From | Volume
Week 3 | Date From | Volume
and so on... without having to manually for each week. I have week number variables set as the business Year starts in July, so Week 1 is the first week in July. Ideally I'd like to use the company weeks but will settle for start of normal year. I've started with this:
SELECT YEARWEEK(date) as weekNum, MIN(sr_mob.`date`) as start_date,
count(*) as numRecords
FROM sr_mob
WHERE outcome='Escalated'
GROUP BY YEARWEEK(date)
This gives me the return data, but the start_date varies depending on when first entry was that week.
Is there any way to define a week in PHP then query the table (which doesn't contain the week numbers) to get what I'm after? Or does this sound like I'll manually have to request each week...
I can run a single query with say:
$Week1 ($week1=20180731-7;)
I guess what I am looking for is a way of doing a for each or while, using the $week variable, without having to write out 52 variables, if that makes sense.
Using reference from: https://stackoverflow.com/a/30373395/2397717
SELECT
YEARWEEK(date) as weekNum,
STR_TO_DATE(CONCAT(YEARWEEK(date),' Monday'), '%X%V %W') as start_date,
count(*) as numRecords
FROM sr_mob
WHERE outcome='Escalated'
GROUP BY YEARWEEK(date)
i got a table named date_use and has a column id & dateUSE.
id dateUSE
1 2015-01-01
2 2015-01-01
3 2015-01-01
4 2015-01-02
5 2015-01-02
now if i will submit another date 2015-01-01 to this table using my php code, it will first count the rows that has the same date and if it is equals to 3 then the date 2015-01-01 that i selected will not be saved and will not be available to be selected.
i want to compare first the date i submitted then if that date has already inputted 3 times in the database then the date i submitted will not be available to be saved and i will just select other date.
just like in checking a reservation date availability, i will select a date then click a button then it will check first my database to see if that date was entered three times then the date i selected will not be available.
You should do this:
SELECT
COUNT(*) AS Total
FROM tablenames
WHERE dateUSE = '$date';
Then in your php code, read this value for total if it is equal 3, then don't do the insert.
You need either a trigger for this or application level logic. Here is an example of application-level logic:
insert into t(date)
select '2015-01-01' as newdate
from (select count(*) as cnt
from t
where date = '2015-01-01'
) x
where x.cnt < 3;
You can try execute this in phpmyadmin if you have installed:
SELECT count(*) FROM date_use WHERE dateUSE='2015-01-02';
after this you will figure out how to implement this in script.
Example: Record A has start date field (12/11/2013) and end date field (14/11/2014). How to get record A if I query using a single date (13/11/2014) query as given.
Record Start_date End_date
A 12/11/2014 14/11/2014
This is for booking facilities system. If a user books a notebook from 12 till 14/11/2014, other users should not be able to book the same notebook on either 12th, 13th or 14th. Using the select statement with the single date, example 13/11/2014, how can we show to the user that the notebook has been booked?
select * from your_table
where '2014-11-13' between start_date and end_date
SQLFiddle
After seeing Juergen d's answer I felt the query was right but after reading the para below the table I felt he needs something like this
Select case when '2014-11-13' between start_date and end_date then 'Booked' else 'Open' end case from your_table
This is little confusing me how to select dates from table on continuous basis.
Suppose i have absent_report table and have entries like this,
Sno Code date
1 101 01-01-2014
2 101 02-01-2014
3 101 03-01-2014
4 101 05-01-2014
5 101 06-01-2014
6 101 07-01-2014
I only want to select continues date from a date. like first three dates not fourth one and so on.
Example Like I have date from which I have to compare is Like 31-12-2013 now from here next continues dates like 01-01-2014,02-01-2014,03-01-2014 only, No matter next records like i have dates in that table 05-01-2014,06-01-2014,07-01-2014. I just want first continuous dates.Hope this clear more what I want.
Use the BETWEEN syntax
SELECT * FROM table WHERE date BETWEEN '2014-01-01' AND '2014-01-03';
You can check if either the next row has the following day or the previous row has the previous day. To do this, I used DATE_ADD() as described in this answer. However, I am not sure if this would be better than implementing the logic in PHP alltogether.
SELECT sno, code, date FROM table AS current
WHERE
DATE(DATE_ADD(date, INTERVAL -1 DAY)) = (SELECT date FROM table AS previous WHERE previous.date < current.date ORDER BY date DESC LIMIT 1)
OR
DATE(DATE_ADD(date, INTERVAL +1 DAY)) = (SELECT date FROM table AS next WHERE next.date > current.date ORDER BY date ASC LIMIT 1)
ORDER BY date ASC
See it work here.
I'm a novice in PHP & MySQL coding and still working my way through it.. So, little help on this will be much appreciated:
I have a table that keeps record of certain events. Here is how my table looks like:
Id - Event_Name - Event_Date - Event_Charges
---------------------------------------------
1 - Event 1 - 10/10/2010 - $100
2 - Event 2 - 10/31/2010 - $200
3 - Event 3 - 11/12/2010 - $150
4 - Event 4 - 12/01/2010 - $175
The objective here is to List the name of Events and total up the charges for a particular Date range...
My front end form looks like this:
From Date: ____________
To Date: ______________
The user enters these two fields and i'm supposed to List the Events falling in that date range, and total up the charges.
Please note that Event_Date field is not of field type "date" in MySQL.. Should i have the Event_Date field of field type "date".
In order to use date operations, the event_date data type needs to be one of:
DATETIME
DATE
TIMESTAMP
Once that's in place, you can use:
SELECT t.event_name,
SUM(t.event_charges) AS total_charges
FROM YOUR_TABLE t
WHERE t.event_date BETWEEN STR_TO_DATE($from_date, '%Y-%m-%d')
AND STR_TO_DATE($to_date, '%Y-%m-%d')
GROUP BY t.event_name
...which brings up the issue of you needing to enforce a date format. That's why I provided an example using STR_TO_DATE, so the string from the HTML form is converted into a MySQL DATETIME data type for comparison.
Ok, so i did some head banging and this is what i came up with:
SELECT t.event_name,
SUM(t.event_charges) AS total_charges
FROM YOUR_TABLE t
WHERE t.event_date1 BETWEEN STR_TO_DATE($from_date, '%Y-%m-%d')
AND STR_TO_DATE($to_date, '%Y-%m-%d')
OR t.event_date2 BETWEEN STR_TO_DATE($from_date, '%Y-%m-%d')
AND STR_TO_DATE($to_date, '%Y-%m-%d')
GROUP BY t.event_name
This works like a charm for my Report! Much thanks!!