ข้ามไปที่เนื้อหาหลัก

PHP สำหรับผู้เริ่มต้น : การเขียน SQL คิวรี่ ค่าน้ำ ค่าไฟ เดือนที่แล้วและเดือนปัจจุบัน ด้วย MIN, MAX

จากบทความที่แล้ว "PHP สำหรับผู้เริ่มต้น : การเขียน SQL คิวรี่ ค่าน้ำ ค่าไฟ เดือนที่แล้วและเดือนปัจจุบัน" ได้แนะนำวิธีการเขียน Query ข้อมูลค่าน้ำค่าไฟเดือนก่อนและเดือนปัจจุบันให้อยู่ในแถวเดียวกัน ด้วยการใช้ SUM ร่วมกับ IF

แต่ผู้เขียนเห็นว่า การใช้ SUM ส่วนใหญ่แล้วจะเป็นการหาผลรวม มากกว่าการหาผลลัพธ์เพียงรายการเดียว ดังนั้นจึงเห็นว่าควรเปลี่ยนไปใช้ MIN กับ MAX เป็นการหาค่าน้อยสุด และมากสุด ตามช่วงเวลาที่ระบุในแบบฟอร์มค้นหา






[ PHP CODE ]


<?php
$user = 'tobedev';
$pass = 'abcd.1234';
?>
<html>
<head>
<title>CODEMANIA.BLOGSPOT.COM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">

<style>

    .highlight {
        background-color: #FFFF88;
    }

    .red_text{
        color : red;
    }
   
    table th,table td{
        text-align: center !important;
    }
</style>
</head>
<body>
    <?php
       
        $year = isset($_GET['year']) ? $_GET['year'] : date('Y');
        $month = isset($_GET['month']) ? $_GET['month'] : date('m');
        if(strlen($month)==1){
            $month = '0'.$month;
        }
        $current_year_month = $year . '-' . $month;
         $prev_year_month = date('Y-m', strtotime('-1 months', strtotime($current_year_month)));
       
       
        try {
            $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

            $sth = $dbh->prepare("SELECT room_id,
                                    MIN(water) AS prev_water,
                                    MAX(water) AS current_water,
                                    MIN(elect) AS prev_elect,
                                    MAX(elect) AS current_elect
                                FROM `tb_meter_list`
                                WHERE DATE_FORMAT( date_record, '%Y-%m' ) BETWEEN '$prev_year_month' AND'$current_year_month'
                                GROUP BY room_id");
                               
            $sth->execute();
            $result = $sth->fetchAll();
       
            $dbh = null;
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
       
        //echo '<pre>', print_r($data, true), '</pre>';
    ?>
       
    <div class="container">
   
    <div class="header clearfix">
        <h3 class="text-muted"><span class="red_text">PHP MySQL</span> : CODEMANIA.BLOGSPOT.COM    </h3>
    </div>
     
    <form class="form-horizontal" method="GET" action="meter_list.php">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">เลือกเดือน : </label>
           
             <input type="text" class="form-control" name="year" placeholder="เดือน" value="<?php echo $year;?>">
             <input type="text" class="form-control" name="month" placeholder="เดือน" value="<?php echo $month;?>">
            <input type="submit" value="ประมวลผล" />
        </div>
    </form>
   
      <div class="row">       
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th rowspan=2>ห้อง</th>
                    <th colspan=3>ค่าน้ำ</th>
                    <th colspan=3>ค่าไฟ</th>
                </tr>
                <tr>
                    <td align="center">เดือนที่แล้ว</td>
                    <td align="center">เดือนปัจจุบัน</td>
                    <td align="center">จำนวนหน่วย</td>
                    <td align="center">เดือนที่แล้ว</td>
                    <td align="center">เดือนปัจจุบัน</td>
                    <td align="center">จำนวนหน่วย</td>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach($result as $row){
                        $water = $row['current_water']-$row['prev_water'];
                        $elect = $row['current_elect']-$row['prev_elect'];
                ?>
                <tr>
                    <td><?php echo $row['room_id'];?></td>
                   
                    <td><?php echo $row['prev_water'];?></td>
                    <td><?php echo $row['current_water'];?></td>
                    <td><?php echo $water;?></td>
                   
                    <td><?php echo $row['prev_elect'];?></td>
                    <td><?php echo $row['current_elect'];?></td>
                    <td><?php echo $elect;?></td>
                </tr>
                <?php }?>
            </tbody>
        </table>
      </div>
     
    </div><!-- container -->

    <footer class="footer">
        <br/><br/>
        <div class="container">
        <i>ติดตามความเคลื่อนไหวได้ที่ :: <a href='https://www.facebook.com/ToBeDeveloper'>https://www.facebook.com/ToBeDeveloper</a></i>
        </div>
    </footer>

</div> <!-- /container -->
</body>
</html>

โครงสร้างฐานข้อมูลสำหรับทดสอบ ดาวน์โหลดได้จากบทความที่แล้ว
"PHP สำหรับผู้เริ่มต้น : การเขียน SQL คิวรี่ ค่าน้ำ ค่าไฟ เดือนที่แล้วและเดือนปัจจุบัน"


PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/

ความคิดเห็น

ค้นหาบล็อกนี้

บทความเดือนล่าสุด