Jump to content

MySql Query help


shahzad429

Recommended Posts

I have a table with fields Table ID and AccessDate when some one visit the page i add date to the database.

so now i want to display click per day or entries to the database per day.

 

 

In  AccessDate date is stored as 27-05-2012

and this field is varchar(255)

 

 

Please help me with this

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/
Share on other sites

Store dates as type DATE and 2012-05-27 format. You cannot correctly sort or compare dates using the format that you have.

 

The query you will then need is

 

SELECT accessdate, COUNT(*) as total FROM tablename GROUP BY accessdate

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1348916
Share on other sites

Thanks bro it is working now.

 

i used below query

SELECT month(accessdate) as Month, COUNT(*) as Total FROM log where LinkCode='dcxha' GROUP BY Month

 

and result is good i can see

Month | Total

4            125

5            87

 

but now i am thinking to show even those month on which clicks are 0 like

is it possible??

 

Month | Total

1            0

2            0

3            0

4            125

5            87

6            0

7            0

8            0

9            0

10          0

11          0

12          0

 

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1348930
Share on other sites

create a temp table containing a row for each month

 

<pre>
<?php
include("testDBconnect.php");
error_reporting(-1);
mysql_query("CREATE TEMPORARY TABLE tempmonths (`month` int NOT NULL PRIMARY KEY)");

for ($m=1; $m<=12; $m++) mysql_query("INSERT INTO tempmonths VALUES ($m)") or die(mysql_error());

$sql = "SELECT m.`month`, COUNT(l.accessdate) as total
    FROM tempmonths m 
    LEFT JOIN log l ON m.`month` = MONTH(l.accessdate)
    GROUP BY m.`month`";
$res = mysql_query($sql) or die(mysql_error());

while (list($m, $tot) = mysql_fetch_row($res))  {
    printf ('%2d  %4d <br />', $m, $tot);
}
?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1348933
Share on other sites

One last question barand  :D

 

where in below query i can write where LinkCode='dcxha'

 

 

SELECT m.month, COUNT(l.accessdate) as total
    FROM tempmonths m 
    LEFT JOIN log l ON m.month = MONTH(l.accessdate)
    GROUP BY m.month

 

as my query before this left join was

 

SELECT month(accessdate) as Month, COUNT(*) as Total FROM log where LinkCode='dcxha' GROUP BY Month

 

as i want to show count for particular LinkCode

 

Thanks again bro you save my life today by helping me this much :)

 

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1348936
Share on other sites

barand today i am working on daily report so query i wrote with dummy table is

 

select d.days as Day, count(l.AccessDate) as Total
from tempdays d
left join log l
on d.days=day(l.AccessDate) and l.linkcode='dcxha' and month(l.AccessDate)=4
group by d.days

 

 

it is displaying date 0 to 31 with clicks working fine

but as i have passed month=4 so april have 30 days

 

so there is any way so that code know how many days to show in each month

 

 

 

thanks,

Shahzad

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1349141
Share on other sites

Selecting month = 4 will only work while you have a single year's data in the table but will be incorrect once you have April data for two or more years.

 

The version below will make it future-proof and also has the advantage that only a single parameter is needed ie the date of the first day in the reporting month (2012-04-01 in this example)

select d.days as Day, count(l.AccessDate) as Total
from tempdays d
left join log l
on d.days=day(l.AccessDate) and l.linkcode='dcxha'
  and EXTRACT(YEAR_MONTH FROM l.AccessDate) = EXTRACT(YEAR_MONTH FROM '2012-04-01')
where d.days <= DAY(LAST_DAY('2012-04-01'))
group by d.days

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1349157
Share on other sites

Barand I have added DATE_SUB(CURDATE(),INTERVAL 1 year) <= l.accessdate in my months query

so that it display months and clicks per month for last year.

 

it is displaying from 1 to 12

as it is May 2012 now so how can i display records from May 2011 to May 2012??

 

 

SELECT m.month, COUNT(l.accessdate) as total
    FROM tempmonths m 
    LEFT JOIN log l
    ON m.month = MONTH(l.accessdate) AND l.linkcode = 'dcxha'  and DATE_SUB(CURDATE(),INTERVAL 1 year) <= l.accessdate
    GROUP BY m.month

Link to comment
https://forums.phpfreaks.com/topic/263198-mysql-query-help/#findComment-1349455
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.