How to show rows by date from now? - mysql

I need a way to show the rows ordered by date ( ascending ) but with the date from now. I am using this query:
SELECT * FROM status
ORDER BY YEAR(datestart), MONTH(datestart), DAY(datestart) ASC
but it still shows the rows older than today.
This is the database structure:
id, status, datestart
database:
0,blabla,2015-02-12 16:15:12
1,blabla,2017-02-12 16:15:12
2,blabla,2016-08-11 19:13:22
4,blabla,2016-01-27 11:12:02
5,blabla,2016-07-21 18:12:02
6,blabla,2018-03-22 13:35:22
8,blabla,2016-08-15 17:12:32
expected results:
5,blabla,2016-07-21 18:12:02
2,blabla,2016-08-11 19:13:22
8,blabla,2016-08-15 17:12:32
1,blabla,2017-02-12 16:15:12
5,blabla,2018-03-22 13:35:22

You can simply add a WHERE clause:
SELECT * FROM status WHERE datestart > now()
ORDER BY YEAR(datestart), MONTH(datestart), DAY(datestart) ASC

Related

Select the latest year in a column using SQL query

I have a dataframe like this
Value.
Date
A
08/08/2009
A
09/12/2021
A
05/10/2022
A
06/09/2022
A
07/08/2022
I need output like
VALUE
DATE
A
05/10/2022
A
06/09/2022
A
07/08/2022
We have to print a latest year with all month data present in the date column .please refer output table.
i used SQL query like
Select Top 10 * from table where
Order by (Date) DESC;
The max() select only one date so that didn't help me
But didn't get expected answer.
Can please someone help me with the query ?
You can just use MAX in a subquery, this will produce the intended outcome you have shown in your question:
SELECT yourcolumn
FROM yourtable
WHERE
YEAR(yourcolumn) = (SELECT MAX(YEAR(yourcolumn)) FROM yourtable);
The latest year is 2022, so MAX in the subquery will find this year and the whole query will select all dates in 2022.
SELECT *
FROM tablename
WHERE datecolumn >= (SELECT DATE_FORMAT(MAX(datecolumn), '%Y-01-01')
FROM tablename)
To improve this query you'd have an index by datecolumn (or where this column is an expression prefix).

SQL: How do I search the data with the LATEST date AND the MOST value in quantity_used?

I know how to search the LATEST date and the MOST value specifically:
Most Quantity Used:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY quantity_used DESC
Latest Date:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY start_date DESC
But how can I do both, by able to search the LATEST date WITH the MOST value in quantity_used? Is this practice even possible?
EDITED: I think my question is not clear enough.
I intend to find the data with the LATEST date first, then from that result FIND the highest VALUE from quantity_used.
I think you just want two order by keys:
SELECT tp.*
FROM tour_packages tp
WHERE tp.active = 1
ORDER BY tp.start_date DESC, tp.quantity_used DESC;
This returns the rows ordered by date and within each date, the ones with the largest quantity go first.

How to use group by, count, and where in MYSQL

I have a table, described like so:
Table1
id (int),
link (varchar512),
text (varchar80),
status (varchar10),
created (timestamp),
updated (timestamp),
user (varchar)
What I need to do is get the total count of rows per user between two timestamps.
So, for example, let's say I want to get the total number of rows for users in the database. That is just a simple
SELECT user, COUNT(*) FROM table_name GROUP BY user;
If I want to get all the rows, for say October, I can do:
SELECT * FROM table_name WHERE created > "2016-10-01 00:00:00" and created < "2016-11-31 23:59:59"
My problem, is I cannot combine the two. I try, and I get syntax errors. I think that I need to run the where query, and then do a count based on that, but I'm not sure how do to that.
Hope this helps.
SELECT user, count(*)
FROM table_name
WHERE created > "2016-10-01 00:00:00" and created < "2016-11-31 23:59:59"
GROUP BY user;
SELECT user, COUNT(*)
FROM table_name
WHERE created >= '2016-10-01'
and created < '2016-12-01'
GROUP BY user;
BTW there is no date 2016-11-31 since November has only 30 days.

Returning closest nearest fields after Now()

I have a query like below
SELECT * FROM mdata ORDER BY ABS(DATEDIFF(NOW(), orderdate)) LIMIT 1
returns the Nearest - Closest Time from a DateTime field type (orderdate) and this includes fields after current time, as well. How can I retrieve the fields AFTER now?
How can I retrieve the fields AFTER now?
This simple query will do that:
-- Return all rows after now
SELECT * FROM mdata WHERE orderdate > NOW()
If you want to return just one after now but not equal to now, do this:
-- Return first row AFTER now
SELECT * FROM mdata WHERE orderdate > NOW() ORDER BY orderdate ASC LIMIT 1
NOW() reference

How to select from a DATETIME column using only a date?

I have a DATETIME column on my table which stores when a record was created. I want to select only the records created on a particular date. If I try:
SELECT *
FROM myTable
WHERE postedOn = '2012-06-06'
It returns no rows even though there are many rows in the table with the postedOn set as
2012-06-06 21:42:02, 2012-06-06 07:55:17 , and so forth.
Any ideas?
Use the DATE scalar:
SELECT *
FROM myTable
WHERE date(postedOn) = '2012-06-06'
SELECT *
FROM myTable
WHERE DATE(postedOn) = '2012-06-06'
DATE() returns the date part of a datetime field.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
Create a time range by adding a day to the date:
SELECT *
FROM myTable
WHERE postedOn >= '2012-06-06' and postedOn < '2012-06-07'
This is the most efficient way, as the query can use an index on the field.