Pull events from database in order from today's date on - mysql

I am building a community site for my fire department and they want to show upcoming events but I don't want to show events that have already passed so here is what I have ( I am using dreamweaver btw).
Database with tables (table = events1532):
-id (int)
-event_start (datetime) YEAR-MO-DA HR:MI:SE
-event_end (datetime) YEAR-MO-DA HR:MI:SE
-event_location (text)
-event_desc (varchar)
I need to get the event start date from the soonest event that has not passed for the front page but I need it in this format = 03/05/2013 at 12:00 AM.
Any ideas guys?

You can use the CONCAT and DATE_FORMAT functions in order to manipulate the result as you want it.
SELECT CONCAT(DATE_FORMAT(event_start, '%d/%m/%Y'), " at ", DATE_FORMAT(event_start, '%h:%m %p')), event_location, event_desc
FROM events1532
WHERE event_start > NOW()
ORDER BY event_start ASC
LIMIT 1;
MySQL Date Format Options
The NOW() function returns a datetime of the current time. You can test this in a MySQL query by simply doing:
SELECT NOW();
The rest is basically ordering the results in ascending order (meaning the soonest date that is greater than NOW() will be at the top of the results), and limiting the result to 1. You could of course limit it to more depending on how many you want returned.

Related

How to get the number of registers by month and date

I have rows of user data. I store the createDate, which is the timestamp in milliseconds when the user registered. I want to get the total number of registrations per month. When I try to do that, I don't get any rows returned. Here's the query I'm using
SELECT COUNT(*) FROM users WHERE YEAR(createDate) = 2023 GROUP BY MONTH(createDate)
createDate is BIGINT and is the date in milliseconds
I guess your createDate column is defined as TIMESTAMP(3), to get millisecond resolution. LAST_DAY() comes in handy here.
Try this:
SELECT COUNT(*), LAST_DAY(createDate) month_ending
FROM users
WHERE createDate >= '2023-01-01'
AND createDate < '2024-01-01'
GROUP BY LAST_DAY(createDate)
The date range test I use for getting the dates in a single year is sargable. That is, it can be accelerated by an index on createDate, where YEAR(createDate) cannot be.
This approach generates a useful result set if you run it on a multi-year date range.
But, if your result set is empty (has no rows), the result set from this query will be too. That might mean:
your table has no dates in that range, or
your createDate data type is something other than TIMESTAMP or DATETIME. (You didn't show us the table definition.)
It sounds like you need to convert to/from unix time:
SELECT COUNT(*), LAST_DAY(FROM_UNIXTIME(createDate/1000)) month_ending
FROM users
WHERE createDate >= UNIX_TIMESTAMP('2023-01-01') * 1000
AND createDate < UNIX_TIMESTAMP('2024-01-01') * 1000
GROUP BY month_ending

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

Rows with a start_date and end_date, need to return records that fall within a date range

I have a table that contains tasks, each task has a date_start and date_finish field.
I need to construct a query which will take a passed in date and return all rows if that passed in date falls between the date_start and date_finish.
Does this make sense?
I have been trying to use standard date type querys such as:
SELECT *
FROM project_task
WHERE project_task.date_start >= '2013-10-10' AND project_task.date_finish <= '2013-10-10'
but it doesn’t return the correct results and using BETWEEN does not work either because I need it to take into account both fields (date_start and date_finish) not just the one.
I think it may only be the WHERE part of the query I need.
You've made a simple mistake, you got the order of your operators wrong:
SELECT *
FROM project_task
WHERE
project_task.date_start <= '2013-10-10' -- start should before the test date
AND
project_task.date_finish >= '2013-10-10' -- and finish after the test date
Explanation
If the date checked is between date_start and date_finish, then we must have reached at last the start date. We could have a later date too. That means the
date_start will be lower or equal than the date checked
And the second check said: the finish date musn't have passed. So
date_finish has to be greater or equal than the date checked.
With your original query you will only get projects that start and end on '2013-10-10'.
Demo
DECLARE #Now datetime = '2013-10-10T00:00:00'
SELECT *
FROM project_task a
WHERE #Now >= a.Date_Start
AND #Now < isnull(a.Date_Finish, '9999-12-31T00:00:00')

mysql query in between dates not working

I have the following mysql table:
tasks:
=====================
tid
status
desc
duedate
And i have the following records in that table:
records
===========================
1
active
Test description
08/15/2014
2
active
Another description
08/31/204
I am trying to get the days that there is a task for, in that particular month. I have the following query but when i run it it gets both records but "day" is null on both of them for some reason. Can someone please help me with this.
MYSQL QUERY
====================
SELECT DATE_FORMAT(due_date,'%d') AS day FROM tasks WHERE due_date BETWEEN '08/01/2014' AND '08/31/2014'
Try:
SELECT DAY(due_date) AS day
FROM tasks
WHERE due_date >= '2014-08'
AND due_date < '2014-09';
DAY() is a better function for what you want and I prefer using >= and < than BETWEEN for date comparisons, as it allows you to specify precise ranges more easily. Here, for example, you don't need to know the number of days in the month.
I have also used the default date format, which is preferable. If you need the, in my opinion, cray American date format, use DATE_FORMAT() in your SELECT.
This will only work with DATE, DATETIME and TIMESTAMP columns, which is how your due_date should be stored, preferably DATE.
UPDATE
To convert the VARCHAR column to DATE run:
UPDATE tasks SET due_date=STR_TO_DATE(due_date,'%m/%d/%Y')
Then change the type. Also remember to change your INSERT statements to use the default format.
You've got to convert those "date" strings to proper date values with STR_TO_DATE:
SELECT
DAY(STR_TO_DATE(due_date,'%m/%d/%Y')) AS day
FROM tasks
WHERE
STR_TO_DATE(due_date, '%m/%d/%Y')
BETWEEN STR_TO_DATE('08/01/2014' '%m/%d/%Y')
AND STR_TO_DATE('08/31/2014', '%m/%d/%Y')
else you're comparing strings instead.
Note:
It would be better to use a proper DATE or DATETIME column instead.
With the current VARCHAR format MySQL is unable to use indexes. That's very bad for performance.
You can convert your data by adding another column to your table:
ALTER TABLE tasks
ADD COLUMN new_due_date DATE;
Then you use an UPDATE statement to fill this new column
UPDATE tasks
SET new_due_date = STR_TO_DATE(due_date, '%m/%d/%Y');
If you don't need your old column anymore then you can delete this column and modify the new column to have the name of the old one. Then you will have your table with all your data in a DATE column.

SQL query to select data between dates does not show last date

im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...