how can i find last date record in MySQL.
i have this current date record but if i want to get a data of last day (not yesterday). it is possible to get last date of record in MySQL.
my current date record are there (28-11-2017)..
and i want last date of record which is (25-11-2017)
between this 25 to 28 there is no data . i want this last date record.
ex.
id DATE PRODUCT_ID
1 2017-11-25 11
2 2017-11-25 12
3 2017-11-24 6
so last date in my table will be 25. how can i get all record of that date.
link for current date table: current table
One option is to compare the date of each record against a non correlated subquery which finds the most recent date in the table.
SELECT *
FROM yourTable
WHERE DATE = (SELECT MAX(DATE) FROM yourTable);
If you are certain that there is only one latest date, or you can live with just a single record in the event of ties, then MySQL offers an even simpler option:
SELECT *
FROM yourTable
ORDER BY DATE DESC
LIMIT 1;
SELECT DATE FROM your_table ORDER BY id DESC LIMIT 1
This will get you the last date.
Related
i have issue.
I need counting more columns where id and date.
date must not be older than 7 days (1 week)
In the picture, we see the ID value as the first and the second value.
I need an SQL query to add the value 10 and 10 = 20 according to ID 344, for example
The same for the second ID. I'm tired already.
Thank you
You must have a date column to get data of last 7 days. So here is my answer after adding a 'date' column.
Answer 1. If you want sum of all the unique id's
select id, sum(object) as value, count(id) as count_of_id, from table_name WHERE date >= DATE(NOW()) - INTERVAL 7 DAY group by id
Answer 2. If you want sum of particular id
select id, sum(object) as value, count(id) as count_of_id, from table_name WHERE date >= DATE(NOW()) - INTERVAL 7 DAY and id = 344
I want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.
I need a record whose year is just before the current year. My table schema and data is like this
aca_id|aca_start_date | aca_end_date
------|---------------|--------------
1 |2013-04-01 |2014-03-31
3 |2015-04-01 |2016-03-31
7 |2014-04-01 |2015-03-31
I want to fetch the record depending on the current year condition is - if current date year is 2014 then fetch the 1st record because its aca_start_date is 2013 which just before 2014. If current date year is 2015 then fetch the record 2nd record. I cannot sort the table on aca_start_date because table is sorted on aca_id in descending order for other purpose.
Need guidance how can I do this job in MySQL query or if this is possible to do by using php
Here is one way:
select t.*
from table t
where year(aca_start_date) = year(now()) - 1
limit 1;
I have a database with a bunch of rows. One of the columns is "date" which has a date for each row in the format: MM/DD/YYYY H:MM PM/AM
How can I write PHP that selects the row where the date is the most recent date that has passed. Meaning if you have a date of tomorrow, a date of today, and a date of yesterday, it picks the row where the date is of yesterday.
I've already connected to the database:
mysql_connect('localhost','username','pass');
mysql_select_db('db_name');
How do I write a query to grab this?
I would do the filtering in SQL rather than PHP, using a variation of the following query:
SELECT *
FROM myTable
WHERE theDate < CURDATE()
ORDER BY theDate DESC
LIMIT 1
This selects all the rows in the past (theDate < CURDATE()), sorts them in reverse chronological order (ORDER BY theDate DESC), then takes the first record (LIMIT 1).
When you query the database, ORDER BY date DESC LIMIT 1
This will only return the most recent result, and thus the result that is closest to the current date. (This only works if you don't put entries that are dated in the future)
Trying to get my head around, If I select a record such as WHERE item_id='$item_id' AND date(datetime)='2012-06-25' and if that record does not exist so I want to get the nearest-latest record after that date. How can I achieve that in a query?
All I can think of the only way right now is if num_of_rows is 0 then I add 3 days period ahead to that day and search again and get the DESC datetime LIMIT 1 (in case there are multiple rows). But who knows I can do it with just a query.
The record could have multiple rows in one day. So if a particular date has no record, how to get the next nearest available data given the same $item_id?
SELECT *
FROM table
WHERE field <= '2012-06-25'
ORDER BY field DESC
LIMIT 1
I think this is what you are looking for:
SELECT *
FROM my_table
WHERE datetime BETWEEN '2012-06-25 00:00:00' AND
DATE_ADD('2012-06-25 00:00:00', INTERVAL 3 DAY)
ORDER BY datetime ASC
LIMIT 1;
also create index on field datetime for faster performance.
This will bring back the item closest to the date that you enter into the query. It won't however look for before or after, just find the closest date to what you enter in.
select
min(abs(DATEDIFF(date(datetime),'2012-06-25'))) as minDiff
,yourID
from table1
group by yourID
order by 1 asc;