I have an events table and need to pull the 4 closest dates to today's date and they can be in the past, present or future.
What would the SQL (using MySQL) be for this if it is possible?
Thanks
Brett
I don't know which DB you are using, but this works with mysql:
select *
from event
order by abs(datediff(event_date, now()))
limit 4
Try using the TIMEDIFF function like this:
select *
from events
order by abs(timediff(now(), yourdatecolumn))
limit 4;
Related
This is my scenario:
I am collecting IP addresses of clients pinging my server and also the time of ping in MySQL.
Columns:
IP_address, Time_of_Ping(date - hr - min - sec)
At any given point of time, I want to know how many clients have been communicated with my server in the last 24 hrs.
Please note: I know this can be done with a simple select statement with where clause.
But why I am posting this in the forum just to know: can this be done with just count() I mean just writing some condition within count() and without using where clause?
Thanks in advance
You could take advantage of MySQL treating boolean expressions as either 1 or 0 in a numeric context and use SUM:
SELECT SUM(Time_of_Ping > NOW() - INTERVAL 24 HOUR)
FROM your_table
I think you want:
select count(*) as num_pings, count(distinct ip_address) as num_ips
from t
where time_of_ping > now() - interval 24 hour;
Note that count(*) is the number of pings. count(distinct) is the number of different ip address, which is what I think you really want.
I got the expected query and it is working fine:
select count(ping_time > datetime('now', '-24 hours')) from testTable
I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)
hope someone can help,
What i`m trying to do is pull all results from the database for any given month,
Is the possible using a mysql query only (no php).
Im using a template app with the only access i have is through i mysql where statement, So i need to work out from "2013-04-01" what the month is and grab it if its the one i want.
The code below works but obviously ties me too the 2013 year.
`SELECT * FROM table_name WHERE Display_Date => 2013-04-01 AND Display_Date <= 2013-04-30 LIMIT 10`
SELECT
*
FROM
table_name
WHERE
MONTH(Display_Date) = MONTH(CURDATE())
AND YEAR(Display_Date) = YEAR(CURDATE())
reference
MONTH()
YEAR()
I'm trying to filter a SELECT query between NOW() and NOW - interval 10 minute(?), but i can't seem to get this to work, and it's given me a few questions on the topic.
I've looked through some documentation online, and alot of questions on stackoverflow but non of the solutions give me what i need. Looking at the TIMEDIFF and TIMESTAMPDIFF documentation, i only see it used like this;
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
However i don't want to just select the time difference, i want to use it in a query as a WHERE clause, something like;
SELECT * FROM tableName WHERE (the time difference betweeen NOW() and the stored timestamp is less than x minutes);
Is there a particular data type i need to set my column to?
How can i use the TIMEDIFF / TIMESTAMPDIFF correctly, and if these are not the correct methods i should be using, what is?
SELECT * FROM tableName WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) < 10
SELECT * FROM tableName
WHERE now() - interval 10 minute < stored_timestamp
In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.