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.
Related
I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.
This is my query it's working fine.
But i want to get the blank record with the missing date, when there is no record exist for that date range, while fetching data from table.
SELECT * from tbl_social where `dCreatedDate` BETWEEN
DATE_ADD('2015-07-06', INTERVAL 3 DAY) and DATE_SUB('2015-07-06', INTERVAL 3 DAY);
if there is no record for date '2015-07-08' i need that date row also in listing.
Thanking you.
You can make some changes to the query and bring all that data which is not in this particular range
and also has no date . to do so try something like this
SELECT * FROM SELECT * from tbl_social
WHERE (
`dCreatedDate` NOT BETWEEN DATE_SUB('2015-07-06', INTERVAL 3 DAY)
AND DATE_ADD('2015-07-06', INTERVAL 3 DAY)
) OR (`dCreatedDate` IS NULL )
with NOT BETWEEN it will seek those records which dont fit the criteria and with NULL it will see for those records whose dCreatedDate is blank and fetch all the data matching both criteria
I have a mysql database in which i have two datetime fields, one is start_date another one is end_date. So lets say scorer1 have a start_date and end_date | similarly scorer2 have a start_date and end_date.
Now how can i get the average days taken by all the scorers?
You can select the difference between the dates with mysql:
SELECT SUM(DATEDIFF(end_date, start_date)) as difference_in_days, COUNT(id) as total_rows FROM table
This will give you the total days difference, and the total rows it applies to.
Then once you've pulled that from the DB, use some PHP to work out the average:
$average = $row['difference_in_days'] / $row['total_rows'];
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;
how can i query my mysql database and fetch rows which are posted in earlier 3 days
i know how to fetch todays's rows but not 3 days ago
time will save in my table like this :
2010-01-20 19:17:49
and this is what i know :
SELECT id FROM pages WHERE date=now()
but i need to show posts in 3days
and im looking for a simple and straight solution ,because i know how to do so in long php codes
To get records three days in the past up to current time:
SELECT t.id
FROM PAGES t
WHERE t.date BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW()
This should select all entries with a date value after and including 3 days ago:
Select id From pages Where NOW() >= Interval date day + 3
Note that if there are dates in the future this will select them too.
Would be helpful to know what version of MySQL you're using and what column type we're talking about but here's the reference of Date and Time Functions for 5.1. You're going to probably want DATE_SUB().