|  | 
	                
  
    | 
	 
        ต้องการนับจำนวน ว่าแต่ละสถานะมีจำนวนเท่าไหร่ ใช้คำสั่ง SQL ประมาณไหนครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | รูปฐานข้อมูล 
 
  
 จะนับจำนวนและ group กันยังไงให้ออกมาแบบนี้ครับ
 
 รูปแบบที่อยากได้
 
 
  
 
 Code (SQL)
 
 
CREATE TABLE `store` (
  `id` int(11) NOT NULL auto_increment,
  `pn` varchar(50) NOT NULL,
  `province` int(2) NOT NULL,
  `status` int(2) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;
-- 
-- Dumping data for table `store`
-- 
INSERT INTO `store` VALUES (1, 'AAA', 10, 1);
INSERT INTO `store` VALUES (2, 'AAA', 10, 2);
INSERT INTO `store` VALUES (3, 'AAA', 10, 3);
INSERT INTO `store` VALUES (4, 'AAA', 10, 4);
INSERT INTO `store` VALUES (5, 'AAA', 10, 5);
INSERT INTO `store` VALUES (6, 'AAA', 10, 1);
INSERT INTO `store` VALUES (7, 'AAA', 10, 2);
INSERT INTO `store` VALUES (8, 'AAA', 10, 2);
INSERT INTO `store` VALUES (9, 'BBB', 10, 1);
INSERT INTO `store` VALUES (10, 'BBB', 10, 1);
INSERT INTO `store` VALUES (11, 'BBB', 10, 1);
INSERT INTO `store` VALUES (12, 'BBB', 10, 2);
 
 
 
 Tag : PHP, MySQL
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2016-03-03 18:19:38 | By :
                          giverplus | View :
                          1522 | Reply :
                          5 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เค้าเรียกว่าการ pivot ครับ เขียน Query ให้แบบครั้งเดียวน่าจะยาก ส่วนมกาเลยใช้การเขียน Query แบบ Group แล้วสร้าง Header ของ Table ก่อน จากนั้นก็ค่อย Loop เพื่อหาข้อมูลอื่น ๆ ที่เกี่ยวข้อง 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-03-04 10:15:28 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ยัง งงๆ อยู่ครับ พอจะมีตัวอย่างที่คล้ายๆ แบบนี้มั้ย ผมลองผิดลองถูกเองไม่ได้เรียนมาด้านนี้เลย 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-03-04 13:38:54 | By :
                            giverplus |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เขียนแบบนี้ถูกหลักมั้ยครับ แต่ผลลัพธ์มันออกตามที่ต้องการแล้ว 
 Code (PHP)
 
 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ทดสอบ</title>
<meta charset="utf-8" />
<style type="text/css">
td{ text-align:center;}
</style>
</head>
<body>
<?php
$link = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("db_test",$link) or die(mysql_error());
mysql_query("set names utf8");
$SQL_PN = "SELECT province,pn FROM store WHERE province=10 GROUP BY pn";
$Query_PN = mysql_query($SQL_PN) or die(mysql_error());
?>
<table border="1" width="670">
<thead>
	<tr>
		<th>province</th>
		<th>pn</th>
		<th>status1</th>
		<th>status2</th>
		<th>status3</th>
		<th>status4</th>
		<th>status5</th>
	</tr>
	</thead>
	<tbody>
	<?php
	while($RS_PN=mysql_fetch_array($Query_PN)){
		$pv = $RS_PN[province];
		$pno = $RS_PN[pn];
		$sql1 = "SELECT COUNT(pn) AS st1 FROM store WHERE pn='$pno' AND status='1' ";
		$sql2 = "SELECT COUNT(pn) AS st2 FROM store WHERE pn='$pno' AND status='2' ";
		$sql3 = "SELECT COUNT(pn) AS st3 FROM store WHERE pn='$pno' AND status='3' ";
		$sql4 = "SELECT COUNT(pn) AS st4 FROM store WHERE pn='$pno' AND status='4' ";
		$sql5 = "SELECT COUNT(pn) AS st5 FROM store WHERE pn='$pno' AND status='5' ";
		$Query_sql1 = mysql_query($sql1) or die(mysql_error());
		$Query_sql2 = mysql_query($sql2) or die(mysql_error());
		$Query_sql3 = mysql_query($sql3) or die(mysql_error());
		$Query_sql4 = mysql_query($sql4) or die(mysql_error());
		$Query_sql5 = mysql_query($sql5) or die(mysql_error());
while($RS_sql1=mysql_fetch_array($Query_sql1)){
	while($RS_sql2=mysql_fetch_array($Query_sql2)){
		while($RS_sql3=mysql_fetch_array($Query_sql3)){
			while($RS_sql4=mysql_fetch_array($Query_sql4)){
				while($RS_sql5=mysql_fetch_array($Query_sql5)){
		?>
		<tr>
			<td><?=$pv;?></td>
			<td><?=$pno;?></td>
			<td><?=$RS_sql1[st1];?></td>
			<td><?=$RS_sql2[st2];?></td>
			<td><?=$RS_sql3[st3];?></td>
			<td><?=$RS_sql4[st4];?></td>
			<td><?=$RS_sql5[st5];?></td>
		</tr>
		<?php
					}
				}
			}
		}
	}
}
		?>
	</tbody>
</table>
</body>
</html>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-03-04 16:12:10 | By :
                            giverplus |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณมากครับ  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-03-04 16:32:16 | By :
                            giverplus |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |