如何在一个表中显示来自不同sql语句的数据? [英] How can I display data in one table from different sql statement?

查看:45
本文介绍了如何在一个表中显示来自不同sql语句的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只做了第一个 sql 语句.请参阅演示链接,了解我到目前为止所做的工作.演示

I Just only done first sql statement. Please see the link for demo what i've done till now. demo

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

I want to show the other three sql statement's result in the other there columns. Check what i've wanted to do and finally want to do sum of the all time slot value. 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 语句.我如何为其他三列应用其他三个 sql 语句.请帮我.提前致谢.

Till now i just apply only 1st sql statement. How can i apply ohter three sql statements for other three columns. Please help me. Thanks in advanced.

推荐答案

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

I cannot test but to make thing easier you can keep your code using 4 queries and change your php code like this (it is not the best solution :) ):

<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>

这篇关于如何在一个表中显示来自不同sql语句的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆