如何在不同的sql语句中显示一个表中的数据?

时间:2014-11-18 10:34:33

标签: php mysql

我只做了第一个sql语句。请参阅演示我迄今为止所做的演示的链接。 demo

我想在其他列中显示另外三个sql语句的结果。检查我想做什么,最后想要做所有时间段值的总和。 demo

到目前为止,我已尝试过以下代码:

            <?php
             $first_time_slot = "00:00:00 - 06:00:00";
             $second_time_slot = "06:01:00 - 12:00:00";
             $third_time_slot = "12:01:00 - 18:00:00";
             $fourth_time_slot = "18:01:00 - 23:59:59";

            $sql1 = mysql_query("SELECT date(`calldate`) as date, count(`src`)as total_caller, sum(`duration`) as total_duration FROM `cdr` WHERE time(`calldate`) BETWEEN '00:00:00' AND '06:00:00' AND `accountcode` = '09614008155' group by date order by date DESC");

            $sql2 = mysql_query("SELECT date(`calldate`) as date, count(`src`)as total_caller, sum(`duration`) as total_duration FROM `cdr` WHERE time(`calldate`) BETWEEN '06:01:00' AND '12:00:00' AND `accountcode` = '09614008155' group by date order by date DESC");

            $sql3 = mysql_query("SELECT date(`calldate`) as date, count(`src`)as total_caller, sum(`duration`) as total_duration FROM `cdr` WHERE time(`calldate`) BETWEEN '12:01:00' AND '18:00:00' AND `accountcode` = '09614008155' group by date order by date DESC");

            $sql4 = mysql_query("SELECT date(`calldate`) as date, count(`src`)as total_caller, sum(`duration`) as total_duration FROM `cdr` WHERE time(`calldate`) BETWEEN '18:01:00' AND '23:59:59' AND `accountcode` = '09614008155' group by date order by date DESC");
            ?>


        <table border="1" width="90%" class="fancy">
            <caption style="color:#FF0000; margin-bottom:10px; font-size:16px;">Showing Caller History.</caption>
            <tr>
                <th>Date</th>
                <th>12am - 6am</th>
                <th>6am - 12pm</th>
                <th>12pm - 6pm</th>
                <th>6pm - 12am</th>
                <th>Total Caller</th>
            </tr>

            <?php if (isset($first_time_slot)){
                while ($row1 = mysql_fetch_assoc($sql1)) { ?>
            <tr>
                        <td><?php echo $row1['date']; ?></td>
                        <td><?php echo $row1['total_caller']; ?></td>

            <?php }} ?>
            </tr>


        </table>

直到现在我只应用第一个sql语句。我如何为其他三列应用ohter三个sql语句。请帮我。提前致谢。

2 个答案:

答案 0 :(得分:0)

正如您所建议的那样,您可以使用这样的一个查询

SELECT date(`calldate`) as date, count(`src`)as total_caller, sum(`duration`) as total_duration
FROM `cdr`
WHERE  (time(`calldate`) BETWEEN '00:00:00' AND '06:00:00')
	Or (time(`calldate`) BETWEEN '06:01:00' AND '12:00:00')
	Or (time(`calldate`) BETWEEN '12:01:00' AND '18:00:00')
	Or (time(`calldate`) BETWEEN '18:01:00' AND '23:59:59')
AND `accountcode` = '09614008155' group by date order by date DESC

答案 1 :(得分:0)

我无法测试,但为了让事情变得更容易,您可以使用4个查询保存代码并更改您的PHP代码(这不是最佳解决方案:)):

<tr>
<?php if (isset($first_time_slot)){
	if ($row1 = mysql_fetch_assoc($sql1)) { ?>
            <td><?php echo $row1['date']; ?></td>
            <td><?php echo $row1['total_duration']; ?></td>    			
	<?php }

	if ($row1 = mysql_fetch_assoc($sql2)) { ?>
            <td><?php echo $row1['total_duration']; ?></td>
	<?php }
	
	if ($row1 = mysql_fetch_assoc($sql3)) { ?>
            <td><?php echo $row1['total_duration']; ?></td>
	<?php }
	
	if ($row1 = mysql_fetch_assoc($sql4)) { ?>
            <td><?php echo $row1['total_duration']; ?></td>
            <td><?php echo $row1['total_caller']; ?></td>
	<?php }

} ?>
</tr>