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!!
Related
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.
I have a table called sales which is displayed below:
s.no item_sold date
1 soap 25.07.2017
2 bisket 19.07.2017
3 chocklate 26.06.2017
4 milk 26.06.2016
Use subdate(current_date, 1)
and BETWEEN.
Eg. To get records between yesterday and day before yesterday.
You can do something like this:
SELECT .. FROM ... WHERE your_date_column BETWEEN
subdate(current_date, 1) AND subdate(current_date, 2)
Uh... I guess...
select * from sales where date between '2017-06-01' and '2017-07-31'
But I think your field name "date" and "s.no" are going to cause you problems...
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
Using a mysql database, I want to select only ContentObject ids from today forward (content objects associated with current and future events). But I'm not sure how to select the month, day, and year from the Events table as a date to compare with today's date. Can someone show me how to do this? Thanks!
I'm going to be using this sql from a php script, so if it's easier to do some work in php, that's fine, too.
Schema:
Events Table:
id |int(11)
attribute_id |int(11)
month |tinyint(4)
day |tinyint(4)
year |int(11)
Attributes Table:
id |int(11)
content_id |int(11)
ContentObject Table:
id |int(11)
Figured it out: This got me what I was looking for:
select E.id, E.year, E.month, E.day
from Events as E
where (E.year*10000 + E.month*100 + E.day) >= (CURDATE() + 0)
The best way is to add date filters into the SQL treatments...
In SQL you have a lot of date function
See MySQL doc here to retrieve month, year, or whatever you wan't...
Example for months :
SELECT * FROM mytable WHERE column_month = DATE_FORMAT(CURRENT_DATE, '%c');
I am trying to create an event calendar with recurring events (ex. weekly or monthly) but I cannot wrap my head around it. Can anyone give me some pointers? What is the best way to go about doing this? Any help is greatly appreciated. Thanks.
Create three tables with a structure like:
event table
-id
-schedule_type
-schedule_id
-title
etc.
schedule table
-id
-event_id
-datetime
schedule_recurring table
-id
-event_id
-date
-time
In your event table, the schedule_type field will be either 0 or 1, which would indicate to the application which table the scheduling information is stored in, as well as how to interpret that information.
A non-recurring event will be stored in schedule with a datetime: 2011-09-06 00:00:00, and recurring events will be stored in schedule_recurring with a date: 'every 1st Monday' and a time: 09:30,12:20 (If the event occurs twice on every first Monday of the month).
Maybe this will help get you started!
I know this is an old post but I was thinking the same thing too.
I like the persons solution about using multiple tables to do it, but in fact it can all be done from one table.
Create a table with the following data...
event_title
event_text
event_image
and_other_fields_about_event
recur_code (text)
recur_mode (integer)
start_date (date)
end_date (date)
recur_end_date (date)
recur_mode can have three states -
0 = no recurrence
1 = recurrence with end date
2 = ongoing with no end date (e.g. if you want to add something like 1st Jan as New Years Day)
recur_code would store either NULL or a code in it if the date recurs. The code that should be used there would be the PHP DateInterval code (i.e. P1Y for 1 year or P3M (3 months) or P7D (7 days), etc) - if you want to shrink the data a bit you could chop off the initial 'P' and add it back later as the initial 'P' is always going to be P, it stands for Period so "Period 3 Months" "Period 7 Days", etc.
Then when your retrieving data from the database - you retrieve all data with the following searches
( end_date >= CURDATE () ) OR ( ( recur_mode = 1 ) AND ( recur_end_date >= CURDATE () ) ) OR ( recur_mode = 2 )
(please note this isn't proper SQL - it's just a basic example of the or statement you'd need)
then once you've retrieved the data use PHP with a while loop and DateInterval to increase the start_date until you get to the next re-occurrence also making sure that if recur_mode is set to 1 the start date is not after the recur_end_date.
All done in one table - and also if you want an input form to put the code in then use a hidden field with the dateinterval value in whilst using various radio buttons to select the interval - then use jQuery onchange to update the hidden value with the new selector values.