Mysql 如何每天显示两个表中的所有时间记录
下面我给出了mysql查询,以输出单日所需的记录。它工作得很好。但我想显示两个表中的所有时间记录。我的两个表都有unix时间戳 单日记录的查询是Mysql 如何每天显示两个表中的所有时间记录,mysql,Mysql,下面我给出了mysql查询,以输出单日所需的记录。它工作得很好。但我想显示两个表中的所有时间记录。我的两个表都有unix时间戳 单日记录的查询是 // DAY DATA STATS $stmt_check = $conn->prepare("SELECT * FROM webstats WHERE pubhash =? AND time BETWEEN $day6 AND $day5"); $stmt_check->bind_param("s", $pubhash); $st
// 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
//日数据统计
$stmt_check=$conn->prepare(“从webstats中选择*,其中pubhash=?和时间介于$day6和$day5之间”);
$stmt\u check->bind\u参数(“s”,$pubhash);
$stmt_check->execute();
$stmt_check->store_result();
$numberofrowsimpressDay=$stmt\u check->num\u行;
$stmt_check->close();
$sql2=“选择SUM(单击)作为单击_sumfrom webstats单击其中的pubhash='$pubhash',时间介于$day6和$day5之间”;
$result2=mysqli_查询($conn,$sql2);
如果(mysqli_num_行($result2)>0){
而($row=mysqli\u fetch\u assoc($result2)){
$clickz=$row['click_sum'];
$clicks=$clickz?:“0”;
}
}否则{
}
?>
||||
我认为你的问题可以通过GROUPBY和DATE()函数来解决。见这个问题:
您还需要使用from_unixtime()将时间戳转换为日期。见这个问题:
编辑:为了帮助您处理特定的情况,可以清理PHP代码,并且您可以在一个查询中而不是两个查询中完成所有操作。见下文:
$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;
$sql2=“选择
将(*)算作“印象”,
unix_时间戳(日期(从_unixtime(A.time)))作为“时间戳”,
将(B.点击)合计为“点击”
从'webstats'作为
左连接'webstatsclick'为B
关于A.pubhash=B.pubhash
其中A.pubhash=“$pubhash”
按日期分组(从_unixtime(A.time))”;
$result2=mysqli_查询($conn,$sql2);
如果(mysqli_num_行($result2)>0){
而($row=mysqli\u fetch\u assoc($result2)){
$impressions[]=$row['impressions']
$clicksall[]=$row['clicks'];
$timestamp[]=$row['timestamp'];
}
虽然($i=0;$i感谢您的回答,但是如何输出DAY MIDNIGHT TIMESTAMP参数,如上文在我的问题中所讨论的,如果我理解您的问题,那么更新的代码应该可以工作。下面是在我的数据库上使用这种查询的示例结果:不,它不工作,它只为最后一天日期生成一条记录。您能解释一下结果吗您得到的是?它只是一行,是返回多行但日期错误?还是其他?请检查我的代码。它只显示一天11月24日的数据,但不显示所有时间
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))
$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;