-1

I have below given mysql query to output required records for single day. It is working perfectly. But i want to show all time records from two table day wise. my two tables have unix timestamps

Query for single day records is

    // DAY DATA STATS
$stmt_check = $conn->prepare("SELECT * FROM webstats WHERE pubhash =? AND time BETWEEN $day6 AND $day5");
$stmt_check->bind_param("s", $pubhash);
$stmt_check->execute();
$stmt_check->store_result();
$numberofrowsimpresssday = $stmt_check->num_rows;

$stmt_check->close();

$sql2 = "SELECT SUM(click) AS click_sum FROM webstatsclick WHERE pubhash='$pubhash' AND time BETWEEN $day6 AND $day5";
         $result2 = mysqli_query($conn, $sql2);

         if (mysqli_num_rows($result2) > 0) {
            while($row = mysqli_fetch_assoc($result2)) {

$clickz = $row['click_sum'];
$clicks = $clickz ?: '0';

            }
         } else {

         }

?>
<br>
<?php echo $day6; ?>||<?php echo $numberofrowsimpresssday; ?>||<?php echo $clicks; ?>
<?php
// DAY STATS ENDS

This should output AS 1542758400||113||2 where DAY MIDNIGHT TIMESTAMP||IMPRESSIONS||CLICKS

Kindly help me. Thanks in advance.

New updated code

$sql2 = "SELECT 
    count(*) as 'impressions',
    unix_timestamp(date(from_unixtime(A.time))) as 'timestamp',
    sum(click) as 'clicks'
FROM `webadstats` as A
LEFT JOIN `webstatsclick` as B
ON A.pubadhash = B.pubadhash
WHERE A.pubadhash='$pubadhash'
GROUP BY date(from_unixtime(A.time))";

$result2 = mysqli_query($conn, $sql2);
if (mysqli_num_rows($result2)>0) {
    while($row = mysqli_fetch_assoc($result2)) {
        $impressions[] = $row['impressions'];
        $clicksall[] = $row['clicks'];
        $timestamp[] = $row['timestamp'];
    }


   for($i=0; $i<count($clicksall); $i++){
    $str .= $timestamp[$i] . '||' . $impressions[$i] . '||' . $clicksall[$i] . ',';
    }
}
echo $str;
Mark
  • 67
  • 7
  • See [Why should I provide an MCVE for what seems to me to be a very simple SQL query](http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Nov 25 '18 at 16:39

1 Answers1

0

I think what you are asking can be solved with GROUP BY and the DATE() function. See this question: MySQL/SQL: Group by date only on a Datetime column

You also need to use from_unixtime() in order to convert the timestamp into a date. See this question: Convert Unix timestamp into human readable date using MySQL

SELECT count(*),
       unix_timestamp(date(from_unixtime(time))) as 'timestamp' 
FROM webstats 
WHERE pubhash =? 
GROUP BY date(from_unixtime(time))

SELECT SUM(click) AS 'click_sum',
       unix_timestamp(date(from_unixtime(time))) as 'timestamp' 
FROM webstatsclick 
WHERE pubhash='$pubhash' 
GROUP BY date(from_unixtime(time))

Edit: To help with your specific case, your PHP code can be cleaned up and you can do everything in one query instead of two. See below:

$sql2 = "SELECT 
    count(*) as 'impressions',
    unix_timestamp(date(from_unixtime(A.time))) as 'timestamp',
    sum(B.click) as 'clicks'
FROM `webstats` as A
LEFT JOIN `webstatsclick` as B
ON A.pubhash = B.pubhash
WHERE A.pubhash='$pubhash'
GROUP BY date(from_unixtime(A.time))";

$result2 = mysqli_query($conn, $sql2);
if (mysqli_num_rows($result2)>0) {
    while($row = mysqli_fetch_assoc($result2)) {
        $impressions[] = $row['impressions']
        $clicksall[] = $row['clicks'];
        $timestamp[] = $row['timestamp'];
    }
    while($i=0; $i<strlen($clicksall); $i++){
        $str .= '<br>' . $timestamp[$i] . '||' . $impressions[$i] . '||' . $clicksall[$i];
    }
}
echo $str;
Jared C
  • 362
  • 7
  • 19
  • thanks for your answer, but how to output DAY MIDNIGHT TIMESTAMP parameter as discussed above in my question – Mark Nov 25 '18 at 16:35
  • If I understand your question, then the updated code should work. Here is an example result using this sort of query on my database: https://i.imgur.com/NtsMAia.png – Jared C Nov 25 '18 at 16:57
  • no its not working, it producing only single record for last day date – Mark Nov 25 '18 at 17:06
  • Could you explain what results you are getting? Is it just 1 row, is it returning multiple rows but the date is wrong? Or something else? – Jared C Nov 25 '18 at 17:08
  • plz check my code https://ibb.co/KLC8M3M. it shows only single day 24 nov day data but not all time – Mark Nov 25 '18 at 17:18
  • First, I don't understand why you are using two different query methods. Dealing with their results will inevitably be different on the php side. Second, the results are not one row, but your loop is dealing with one variable and overwriting the results on each pass of the loop. That's why your result just shows you the last result. I'll type up a longer php code, give me a few minutes please. – Jared C Nov 25 '18 at 17:39
  • My updated code should be able to replace the entire portion which you showed in your screenshot. – Jared C Nov 25 '18 at 18:11
  • Mark, you need to provide more details. Please show the code as you did before (so I can see line numbers) and then please show the exact error. – Jared C Nov 27 '18 at 19:03
  • Jared i have updated my question with new code, I have rectified its syntex error but i have issue, this code is generating same values for both $impressions[$i] and $clicksall[$i] variables. Kindly help – Mark Nov 28 '18 at 16:51
  • Is the value for impressions correct, or clicks? Can you please tell me the data structure for the click column in the webstatsclick table? – Jared C Nov 28 '18 at 17:45
  • The value of impressions is correct. The table webadstats contains impressions data and webstatsclic clicks data which has columns srno, pubadhash,time and click column having value 1. – Mark Nov 28 '18 at 19:52
  • Try changing sum(B.click) to count(B.click) – Jared C Nov 28 '18 at 20:54
  • Tried both with sum(B.click) to count(B.click) but still issue persists – Mark Nov 29 '18 at 16:07
  • Can you please show some rows of your database? The code works for me when I test with my own tables – Jared C Nov 29 '18 at 17:40
  • Plz mail me your email at psjallah1@gmail.com so that we can discuss there – Mark Nov 29 '18 at 18:03
  • After reviewing the images you sent me and the code which works on my reproduced tables, I wonder if your click column is set to some default value and if that is influencing the result. Instead of count(B.click) try count(B.visitorip). If that still doesn't work, please screenshot the entire section of code once again. – Jared C Dec 01 '18 at 00:40
  • Jared plz check emailed screenshots – Mark Dec 01 '18 at 09:50
  • What happens if you change `ON A.pubadhash = B.pubadhash` to `ON A.session = B.session` ? – Jared C Dec 01 '18 at 09:52