Select from mysql - php

I have a table like below:
uid nid points date reason
36 116 2 2012-08-28 11:52:12 session
31 110 2 2012-08-23 15:47:47 session
36 115 2 2012-08-27 11:52:48 session
as u can see uid is not unique, it can be repeated. What i need is to select sum of points for every id between date(30 days before). For example: sum of uid36 = to 4.
What i have tried:
$start_time = mktime(0, 0, 0, $today["mon"], ($today["mday"] - 30), $today["year"]);//30 days before
$end_time = time();
$query = db_query("SELECT uid,sum(points), date FROM users_history WHERE date BETWEEN '$start_time' AND '$end_time'");
but how to select for every id

You need GROUP BY uid, and date is meanless in your query if you are using aggregate function SUM.
SELECT uid, SUM(points)
FROM users_history
WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 day) AND NOW()
GROUP BY uid
And if there is no future date, then you only need
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 30 day)

for that you need to group your uid.
SELECT uid,sum(points), date FROM users_history WHERE date BETWEEN '$start_time' AND '$end_time'" group by uid;

SELECT uid, SUM(points) FROM users_history WHERE date BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW() GROUP BY uid

Related

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

MySQL retrieve data for the past 24 hours when the column table is of type date NOT timestamp

I am trying to retrieve data for the passed 24 hours but I am getting nothing, but when I check on the table column (days) directly in db, I can find them. Here is my query:
select * from mytable
where
days between concat(date(date_sub(now(), interval 1 day))
between requires 2 arguments that specify the range:
select * from mytable
where days between date_sub(now(), interval 1 day) and now();
Or simply
select * from mytable
where days > date_sub(now(), interval 1 day);
You can use
select * from mytable where days > date_sub(CURDATE(),INTERVAL 1 DAY);

mysql group_concat showing all dates

I have 2 tables:
profile userid, fname,lname
schedule: userid, date, start_time, end_time
Here is my query .
select *, group_CONCAT(distinct(date),'_',start_time,'_',end_time)
as dateformat FROM profile, schedule WHERE profile.userid =
schedule.pid
This returns all dates from the table. But what I am trying to do is return only certain days like:
date >= DATE(NOW()) and date <= date(now() + 2 day)
So today tomorrow and the day after.
No matter what I try it just returns all dates in the schedule database. I need this to return 2 rows userid 5 has 1 row in profile but 5 rows in schedule and userid 6 has 1 row in profile and 2 rows in schedule the query works fine its just returning all dates and not the dates within the
"date >= DATE(NOW()) and date <= date(now() + 2 day)"
did you try GROUP BY?
like WHERE profile.userid = schedule.pid GROUP BY date
select profile.userid,schedule.userid, group_CONCAT(distinct(date),'_',start_time,'_',end_time) as dateformat FROM profile, schedule WHERE profile.userid = schedule.userid and date >= DATE(NOW()) and date <= date(now() + interval 2 day);
That did it for now!! Now I can use the dateformat variable in php to parse out my results.
kanishka panamaldeniya - thanks for your suggestion!

What should be MySQL query for the following?

I wish to show upcoming birthday records on dashboard. I have MySQL table 'contacts' which
has field dob (DATE YYYY-MM-DD). I want to retrieve all the records with birthday is coming
in next 15 days. e.g is: birthday is 1986-03-24 it should be in result.
Take a look at MySQL DATE_ADD and INTERVAL.
By using both as below you can get record that are coming in next 15 days.
SELECT * FROM user WHERE dob BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY)
FOR birthrate comparison.
MAKEDATE(YEAR(NOW() + INTERVAL 15 DAY), DAYOFYEAR(birthday)) BETWEEN CURDATE() AND CURDATE() + INTERVAL 15 DAY
SQLFiddle Demo
You have to use the date in mysql query as:
SELECT FROM table_name WHERE timestamp_row BETWEEN
'2011-01-01 00:00:00' AND '2011-01-02 23:59:59';
You are doing the B'Day dates of users and thus hope this would be do the trick of formatting and selection of date
select id from my_table
where
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),
'%Y%m%d000000' ) AND timestamp >= date_format(CURRENT_TIMESTAMP(),
'%Y%m%d000000')
Can do by this way also
SELECT * FROM person WHERE IF ( MONTH( NOW() ) < 12,
MONTH( birthdate ) = MONTH( NOW() ) + 1, MONTH( birthdate ) = 1)
You can use this query. Try Out
SELECT * FROM TableName
WHERE REPLACE(BirthDate, DATEPART(YEAR, BirthDate), DATEPART(YEAR, GETDATE())) BETWEEN GETDATE() AND DATEADD(DAY, 15, GETDATE())

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories