php - get different dates in while loop - php

I have this while loop:
<?php
$q=mysql_query("SELECT * FROM xeon_stats_clicks WHERE user='".$userdata['username']."' AND typ='4' ORDER BY data DESC LIMIT 8") or die(mysql_error());
while($clickData=mysql_fetch_assoc($q)):
$r=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='3' AND data='".date("Y/m/d")."' ORDER BY data DESC LIMIT 8");
echo mysql_result($r, 0);
endwhile;
?>
This will just give me the sum of the value row for today only. How can I do so I get the data from the last 7 days?

there is no need to specify the certain date for it will give you on this specific date, another comment suggested the use of 'between' but I like the use of '>' only in this case,
for example:
SELECT sum(value) FROM xeon_stats_clicks WHERE
user=.$userdata['username'] AND type='3' AND date > CURDATE() -
INTERVAL 1 WEEK ORDER BY date DESC LIMIT 8"

SELECT SUM( value ), `data`
FROM table
WHERE ( `data` BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 WEEK )
GROUP BY `data`
for example.
try another where statement:
DATEDIFF( CURDATE(), CURDATE() - INTERVAL 1 WEEK ) = 7
and show your where statement

Related

How to check if user have X records from last 1 hour? [PHP/MySQL]

I want to check if the user has 5 records in my MySQL database in the last hour. That´s how I am doing it now:
$link = mysqli_query($link, "SELECT * FROM find_points WHERE timestamp > '.time()-3600.' AND user_id = '1' ORDER BY id DESC");
if(mysqli_num_rows($link) >= 5) {
echo 'more than 5 results';
}
It looks like it should work, but it doesn't work...
Please use below query
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, timestamp , now() ) > 1 AND user_id = '1' ORDER BY id DESC
You can read in manual about TimestampDiff
It can be used to run difference between 2 dates in various formats.
Please check the Demo on SqlFiddle
It shows how TimestampDiff returns result and you can use the same in WHERE clause.
Update
Based on your comment, that the timestamp is stored as Unix Timestamp, you could use the following query:
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, FROM_UNIXTIME(timestamp) , now() ) > 1 AND user_id = '1' ORDER BY id DESC
FROM_UNIXTIME will convert your UNIX Timestamp to DateTime Format. You can then pass this to TIMESTAMPDIFF which will calculate difference and return the number of hours.
Hope this helps.
"SELECT * FROM find_points WHERE timestamp > DATE_ADD(NOW(), INTERVAL 1 HOUR) AND user_id = '1' ORDER BY id DESC"

how can i write as one query to get count in mysql

I want to get the today count of users and yesterday's users count for that i want to write only one query how can i do that..?
these are my queries I want only one query:
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,1 as todayCount
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,0 as yesterdayCount
My expected results or only 2 columns
todayCount yesterdayCount
2 4
This should do the trick:
SELECT COUNT(CASE
WHEN visited_date = CURDATE() THEN 1
END) AS todayCount ,
COUNT(CASE
WHEN visited_date = CURDATE() - INTERVAL 1 DAY THEN 1
END) AS yesterdayCount
FROM visitors
WHERE visited_date IN (CURDATE(), CURDATE() - INTERVAL 1 DAY)
GROUP BY visited_date
ORDER by visited_date
If you know the current and previous date, then you can do:
SELECT SUM(visited_date = CURDATE()) as today,
SUM(visited_date = CURDATE() - interval 1 day) as yesterday
FROM visitors
WHERE visited_date >= CURDATE() - interval 1 day;
If you don't know the two days, then you can do something similar, getting the latest date in the data:
SELECT SUM(v.visited_date = m.max_vd) as today,
SUM(v.visited_date < m.max_vd) as yesterday
FROM visitors v CROSS JOIN
(SELECT MAX(v2.visited_date) as max_vd FROM visitors v2) as m
WHERE v.visited_date >= m.max_vd - interval 1 day
Just try this simple query
select visited_date as date, COUNT(*) as count from `visitors`
group by `visited_date` order by `visited_date` asc
It will produce output as
It will work for you.
Try this:
$sqlToday = "Select COUNT(*) FROM menjava WHERE DATE(date_submitted)=CURRENT_DATE()";
$sqlYesterday = "Select COUNT(*) FROM menjava WHERE DATE(dc_created) = CURDATE() - INTERVAL 1 DAY";

sql order by most recent dates

I have this table in my database:
INSERT INTO `shop_stats` (`date`, `value`) VALUES
('09/2014', 326),
('08/2014', 1007),
('07/2014', 1108),
('06/2014', 1027),
('05/2014', 895),
('04/2014', 650),
('03/2014', 683),
('02/2014', 563),
('01/2014', 499),
('12/2013', 568),
('11/2013', 522),
('10/2013', 371),
('09/2013', 347),
('08/2013', 376),
('07/2013', 418),
('06/2013', 567),
('05/2013', 357);
i need to find a way to display the last 12 months.
I tried this:
SELECT * FROM shop_stats ORDER BY date DESC LIMIT 12
But it doesn't work correctly.
Any suggestions ?
SELECT * FROM shop_stats WHERE date >= DATE_SUB(NOW(),INTERVAL 1 YEAR) LIMIT 12
Your "dates" are stored as strings, presumably with the month first. So, the following order by should work
order by right(date, 4), left(date, 2)
You need to put the year before the month for ordering purposes.
If you want the last twelve months, I would recommend:
where right(date, 4) * 12 + left(date, 2) >= year(now()) * 12 + month(now())
order by right(date, 4), left(date, 2)
The where statement converts the dates to a number of months, for both the "date" column in your data and for the current time.
You can simply use STR_TO_DATE like this
SELECT
*
FROM
shop_stats
order by
STR_TO_DATE(date, '%m/%Y') DESC LIMIT 12
Demo
I suppose your field date has a type string
So you try this:
SELECT * FROM shop_stats
ORDER BY SUBSTRING(date, 4, 4) desc,
substring(date, 1, 2) DESC LIMIT 12
Show Sql Fiddle
You can take a look at DATE_SUB
SELECT * FROM shop_stats where date >= DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY date
Edit:
How about converting the string to date & doing the appropriate date operations in the sql ?
SELECT DATE
,t1.value
FROM (
SELECT DATE
,STR_TO_DATE(CONCAT (
'01/'
,DATE
), '%d/%m/%Y') date_
,value
FROM shop_stats
) t1
WHERE t1.date_ >= DATE_SUB(now(), INTERVAL 12 MONTH)
ORDER BY t1.date_ DESC
http://sqlfiddle.com/#!2/2be05/8
select * from shop_stats where date >= (NOW() - INTERVAL 12 MONTH) ORDER BY date

Specify PHP code to select dates and records 3 days old

I am trying to call data from SQL table that is only 3 days old
My table has a lbs-date column in it and is date format. I have tried the following but get no result from the query at all
$result = mysql_query("SELECT *, DATE_FORMAT(datetime, '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date(datetime) = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC")
Is there any other way I can call only the last 3 days of information from the SQL my date format is Y/M/D
SELECT *, DATE_FORMAT(lbs_date, '%y,%m,%d')
FROM lbs_trace_etrack
WHERE lbs_date >= CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
check DATE_FORMAT. Its syntax is DATE_FORMAT(<date>,format) . Use like this :
SELECT *, DATE_FORMAT(lbs_date , '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC

PHP / MySQL - Construct a SQL query

Im having a little trouble constructing a query.
I have a table with 3 columns.
id - day - pageviews
What i basically want to do is get 8 id's from the table where the pageviews are the highest from the last 60 days.
The day column is a datetime mysql type.
Any help would be great, im having a little trouble figuring this one out.
Cheers,
Almost the same as TuteC posted, but you'll need a group by to get what you need...
SELECT id, SUM(pageviews) totalViews
FROM table
WHERE DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= day
GROUP BY id
ORDER BY totalViews DESC
LIMIT 8
Do something like this:
SELECT id FROM table_name
WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
ORDER BY pageviews DESC
LIMIT 8;
$sixtyDaysAgo = date('Y-m-d',strtotime('-60 days'));
$sql = "SELECT id
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
ORDER BY pageviews DESC
LIMIT 8";
If each row is a number of pageviews for that day, and you're looking for the highest total sum of 60 days' worth, then you'll need to total them all and then grab the top 8 from among those totals, like so:
$sql = "SELECT id
FROM (
SELECT id, SUM(pageviews) AS total_pageviews
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
GROUP BY id
) AS subselect
ORDER BY total_pageviews DESC
LIMIT 8";

Categories