how i group do this mysql query - php

i want to make charts system
and i think it must be like that
1 jan 2009 = 10 post
2 jan 2009 = 2 post
4 jan 2009 = 10 post
6 jan 2009 = 60 post
and i have posts table that has id,user_id,date
how i can select from posts to show it like that

Try ..
SELECT DATE_FORMAT(`date`, '%e %M %Y') as `post_date`, COUNT(*)
FROM your_table
GROUP BY `post_date`
More information to reference:
DATE_FORMAT
Aggregate Functions

Related

How to select a php date() format in SQL

I have an SQL table with a field "time_created" in the form "Wed, 19 Aug 2015 03:58:00 -0600".
I need to check this field against today and perform a where statement according to it, as in
SELECT * FROM table WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_created) >= 3600*24*30
to select records where time_created is older then a month.
Problem is UNIX_TIMESTAMP doesn't understand formats as "Wed, 19 Aug 2015 03:58:00 -0600". Any other way?
Try:
SELECT *
FROM table
WHERE DATE(NOW()) > DATE_SUB(STR_TO_DATE(time_created, '%a, %e %b %Y %T'), INTERVAL 1 MONTH)
This will find all records where the current date (DATE(NOW())) is bigger than time_created subtract ` month.

Use Unix timestamp as a condition in mysql

I have a table that is similar to the one below. As you can see, I have stored the dates as Unix timestamps. My goal is to only return the results which are for the upcoming dates, not the ones which are in past. How can I achieve this?
id | date
1 1331506800 //Mar 12 2012
2 1366149600 //Apr 17 2013
3 1413928800 //Oct 22 2014
4 1436652000 //Jul 12 2015
Desire result:
id | date
1 1413928800 //Oct 22 2014
2 1436652000 //Jul 12 2015
SELECT *
FROM table
WHERE date > UNIX_TIMESTAMP()
$h->query("SELECT * FROM thetable WHERE date > " . time());
This will also work in other databases than MySQL which do not have UNIX_TIMESTAMP() function.
Try this::
Select * from table
where DATEDIFF(STR_TO_DATE(dateColumn, '%e %b %Y %k:%i')),now())>0

mysql query - one query to create table showing sales by month

I have a mysql table with sales information: Seller, product, sales date.
How do I write a query to generate a table that shows total products shown by each seller, per month, and in total?
This is the query I use to create a table with SellerName and total sales in July:
select SellerName, count(*) as july
from onlineDATA
where ReportMonth = 201207
GROUP BY SellerName
ORDER BY july DESC
LIMIT 0,30
How do I modify this to get additional sales totals for each month?
I am looking for output like this:
SellerName | Jun | Jul | Aug | YTD
John Doe | 30 | 25 | 30 | 85
Bonus question - how would I code this in PHP when the number of months would be a user input - anywhere from 1 to 12?
Thanks
I think you will have to define the columns separately, like this:
SELECT
SellerName,
SUM(IF(ReportMonth = 201206, 1.0)) AS Jun,
SUM(IF(ReportMonth = 201207, 1.0)) AS Jul,
SUM(IF(ReportMonth = 201208, 1.0)) AS Aug,
COUNT(*) AS YTD
FROM onlineDATA
GROUP BY SellerName
ORDER BY SellerName DESC LIMIT 30;

Embedded SQL request

I have a SQL Database. In here is a table called Reports and it looks like this
ID Date Year Title Links
1 2010-05-03 2010 Report 1 link1.php
2 2010-09-03 2010 Report 2 link2.php
3 2011-01-05 2011 Report 3 link3.php
What I'm trying to achieve it the this. I want to select all the reports of 2010-2011 but the ones from 2010 may only be those dating from september.
Right now, I've got a SQL statement selecting all the reports of the two years
$sql = "SELECT * FROM Reports WHERE Year = ".$db->escape($jaar1)." OR jaar = ".$db->escape($jaar2);
How can I select Report 2 (cause it dates from september 2010) and Report 3 (cause it dates from 2011)?
The easiest way as I see it, is to use the BETWEEN...AND operator.
SELECT *
FROM `Reports`
WHERE `Date` BETWEEN '2010-09-01' AND NOW()
Note the backticks. Especially important around Date, because it is a reserved word.
Assuming the Date column is of type DATE:
SELECT *
FROM `Reports`
WHERE (YEAR(`Date`) = 2010 AND MONTH(`Date`) = 9)
OR YEAR(`Date`) = 2011
general pattern
where year = 2011
or ( year = 2010 and month = 'September' )

Query returns Date (YYYY-MM-DD) and want to display as Friday, Oct 15

I have a query that is grabbing the Date of a given post. I'm displaying this information in a chart that has the date and number of posts for that day. How can I display "Friday, Oct 15" instead of 2010-10-15??
Query:
$oct_week = mysql_query("SELECT COUNT(*), DATE(`dPostDateTime`) AS `day` FROM `tblQA` WHERE dPostDateTime >= '2010-10-01' AND dPostDateTime <= '2010-10-31' GROUP BY `day`");
DATE_FORMAT(DATE(`dPostDateTime`), '%W, %b, %e')

Categories