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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.