Can someone tell me how to select the most recent date (with the least different) from now?
Currently I'm using something like this
SELECT MAX(DatumAangemaakt) AS RecenteSchema FROM Personeelsschema
I use the Max() function, but it's no good because if I have a date in the future (e.g. 2015-06-23) this date will be selected.
And what I need is to select today's date (if there any) or the most recent date from today, e.g:
2015-06-23
2015-06-01
2015-05-17 (this one will be selected)
Is there a way to do that?
Get all dates past the current date, order them and get the top record:
SELECT DatumAangemaakt AS RecenteSchema
FROM Personeelsschema
WHERE DATE(DatumAangemaakt) >= DATE(NOW())
ORDER BY DatumAangemaakt LIMIT 1
SQL Fiddle Demo
If you also want to include dates from the past in your proximity check, then you can use the following query:
SELECT DatumAangemaakt
FROM Personeelsschema
ORDER BY ABS(TIMESTAMPDIFF(DAY, DatumAangemaakt, DATE(NOW()))) LIMIT 1
SQL Fiddle Demo
Related
I need to fetch all data from table as per date field in descending order using PHP and Mysql. I am explaining my table below.
db_special:
id name date
1 Ram 12/04/2017 15:31:57 PM
2 Raj 12/03/2017 05:31:57 AM
3 Rahul 11/28/2017 12:30:54 PM
Here is my query:
select *
from db_special
where
STR_TO_DATE(date, '%m/%d/%Y')
BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
Here I implemented one logic to fetch last 7 days data and now I need to implement sorting as per the same date field in descending order using MySQL and PHP.
Are you really using a VARCHAR/CHAR/CLOB field to store a date? That is very bad. It makes kittens cry. Please don't do it.
using PHP and Mysql
No. you don't retrieve data from a DBMS then sort it in PHP. That too is very bad (unless you're Facebook, in which case it is possibly a bit silly, but that is another story). You sort in the database.
Just add an ORDER BY clause at the end of your SELECT
I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;
I have a table that inserts a row when a certain function is used on the site and stamps the date.
code-/----date----/--type
-----/------------/------
--1--/--2012-2-1--/-used
--1--/--2012-2-3--/-saved
--1--/--2012-1-3--/-printed
--2--/--2012-2-1--/-used
--2--/--2012-2-2--/-printed
I have to report the number of times code 1 was (printed or saved or used) today, or yesterday, or last month (date range)
I am starting with this:
$stat_query = mysql_query("SELECT code, type, date FROM tracking WHERE code IN ('$htL','$htG','$htR') GROUP BY code, type, date");
I use the IN operand because each user has 3 codes to track with limitless date entries for each type.
I really am lost as to what to do here.
You'll need to construct the date ranges and modify the query. Below is an example of showing the count for code in the past day:
SELECT `code`, COUNT(`code`) as code_cnt
FROM tracking
WHERE
`code` IN ('$htL','$htG','$htR') AND
`date` BETWEEN DATE_SUB(now(), INTERVAL 1 HOUR) AND now()
GROUP BY `code`
Check out the DATE_SUB documentation for further help
GROUP BY code, type, date in this example makes it to return only one record. Because all code rows would be summerized in distinct output row, also type would be reduce more and more. Please check your GROUP BY fields.
I have a list of dates in a table in a MySQL database (the dates when a charity bookstall is to be held), which I want to display on a page. On one page I'm displaying the date of the next stall, and on another the dates of the stall in the next month. (Currently I'm using an unordered HTML list and selecting the dates with PHP, but it's a bit messy, and I also want to tie in the dates with the fundraising totals that are stored in the database).
I want to put the dates in a database though so that I can tie in the dates with the fundraising totals for each week. I'm thinking that once I can identify the date with the nearest up-coming date that I can use 'LIMIT 1' to select the next week's date for display, and 'LIMIT 4' say for where I need to display the dates for the next month, but what I can't figure out is how to identify the record with the nearest up-coming date - identifying the current date and then selecting the nearest date...I have a feeling there's probably one of the MySQL date functions that can be persuaded to help out in this, but can't figure out exactly how.
Any ideas on how I can do this?
If I understand correctly, you can just pick up next four dates that are after today.
In MySQL you could use the CURDATE() function for the 'today' bit, then apply an order and limit to your select statement. For example,
SELECT stall_date
FROM stall_dates
WHERE stall_date >= CURDATE() -- >= assumes you want today's to show too
ORDER BY
stall_date
LIMIT 4
Use ORDER BY stall_date DESC to reverse the ordering if needed.
If your column is a DATETIME field, you can identify the next by using SELECT...WHERE event_date > "2009-11-06" and ORDER BY event_date.
SELECT * FROM so_events
WHERE event_date > "2009-11-06 15:36:00"
ORDER BY event_date ASC
LIMIT 4
MySQL will internally do the work for you and select rows where whose timestamp is greater than the one you specify in the WHERE clause.
I am a complete novice at SQL Server and naively thought that there would be a QUARTER() function, alas there is not and some googling didn't come up with anything useful!
Basically what I want to achieve is that for all rows in my database I want a count of those rows grouped by Quarter.
If possible I would like to keep all calculation with the query but if not it is PHP that is kicking everything off.
This should do the trick, provided that you have a datetime column in the row of course :)
SELECT datepart(qq, my_date_column) as quarter, count(*) as rows
from my_table
group by datepart(qq, my_date_column)
Of course, if you have more than one years data, you might want to add datepart(yyyy, my_date_column) as well
I use
select Datename(quarter,MYDATECOLUMN_NAME)
MYDATECOLUMN_NAME is date format.
I simply used the Month Function on the date and then used a Case function to organize them:
Case When Month(date) in (1,2,3) then '1stQtr'
When Month(date) in (4,5,6) then '2ndQtr'
When Month(Date) in (7,8,9) then "3rdQtr'
When Month(Date) in (10,11,12) then '4thQty' END AS Quarter
select
CONVERT(DATETIME, CONVERT(CHAR(8),
DATEPART(YEAR, yourDateColumn) * 10000 +
( DATEPART(QUARTER, #date) * 3 - 2) * 100 +
1),
112) from...
taken from here:
http://geertverhoeven.blogspot.com/2007/01/get-quarter-of-given-date-in-datetime.html
Or you could the SQL mentioned in the comment in that link, to get the first day of the quarter:
DATEADD(qq,DATEDIFF(qq,0,#date),0)