| 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (PHP) 
 SELECT count(id) FROM ages where year(age) between  year(now())-20 and year(now())-15
 
 นับเป็นปีทหารเอานะ 555
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-17 04:33:48 | By :
                            LindyFralin |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | สร้างตารางแล้วลอง run query ข้างล่างดูครับ 
 
 CREATE TABLE `user_profiles` (
 `id` int(11) NOT NULL auto_increment,
 `nm` varchar(50) NOT NULL,
 `bd` date NOT NULL,
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
 
 
 select
count(*) as age_count,
group_concat(nm) as names,
current_date,
bd,
year(current_date)-year(bd) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) as age
from user_profiles
group by
year(current_date)-year(bd) + if(date_format(current_date,'%m%d') > date_format(bd,'%m%d'),1,0)
 
 
 
 SELECT
group_concat( NAMES ) AS age_range_names,
sum( age_count ) AS age_range_count,
if( age >=60, 'more than 60', if( age >=50, '50-59', if( age >=40, '40-49', 'less than 40' ) ) ) AS age_range_count
FROM
(SELECT
count( * ) AS age_count,
group_concat( nm ) AS NAMES ,
current_date,
bd,
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) AS age
FROM user_profiles
GROUP BY
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 )
) AS user_ages
GROUP BY if( age >=60, 'C6', if( age >=50, 'C5', if( age >=40, 'C4', 'C3' ) ) )
 
 อ่านวิธีการเขียน query ที่ซับซ้อนได้ที่
 http://www.firstmeditech.com/cakephp/post/mysql-complex-query
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-17 05:36:36 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลืมให้ข้อมูลทดสอบครับ 
 INSERT INTO `user_profiles` VALUES (1, 'n1', '1944-04-19');
 INSERT INTO `user_profiles` VALUES (2, 'n2', '1945-04-19');
 INSERT INTO `user_profiles` VALUES (3, 'n3', '1948-04-19');
 INSERT INTO `user_profiles` VALUES (4, 'n4', '1955-04-19');
 INSERT INTO `user_profiles` VALUES (5, 'n5', '1958-04-19');
 INSERT INTO `user_profiles` VALUES (6, 'n6', '1959-04-19');
 INSERT INTO `user_profiles` VALUES (7, 'n7', '1966-04-19');
 INSERT INTO `user_profiles` VALUES (11, 'n10', '1966-04-19');
 INSERT INTO `user_profiles` VALUES (9, 'n8', '1948-04-19');
 INSERT INTO `user_profiles` VALUES (10, 'n9', '1943-04-19');
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-17 05:46:29 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เอิ๊ก ๆ ผมว่าแล้ว ว่าทำไมเว็บเก่าคุณหนุ่มเข้าไม่ได้
 ที่แท้ ย้ายโฮสต์ โอเค ผมได้ที่เข้าไปสูบความรู้แล้ว อิอิ
 
 ยินดีด้วยครับคุณหนุ่ม
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-17 08:27:37 | By :
                            deawx |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  Quote: เอิ๊ก ๆผมว่าแล้ว ว่าทำไมเว็บเก่าคุณหนุ่มเข้าไม่ได้
 ที่แท้ ย้ายโฮสต์ โอเค ผมได้ที่เข้าไปสูบความรู้แล้ว อิอิ
 
 ยินดีด้วยครับคุณหนุ่ม
ขอบคุณคับ
 
 
 
 โค้ดข้างบน ผมเขียนคำสั่งคำนวนผิดไป
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) AS age
 ที่ถูกคือ
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 ) AS age
 
 
 SELECT
group_concat( NAMES ) AS age_range_names,
sum( age_count ) AS age_range_count,
if( age >=60, 'more than 60', if( age >=50, '50-59', if( age >=40, '40-49', 'less than 40' ) ) ) AS age_range_count
FROM
(SELECT
count( * ) AS age_count,
group_concat( nm ) AS NAMES ,
current_date,
bd,
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 ) AS age
FROM user_profiles
GROUP BY
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 )
) AS user_ages
GROUP BY if( age >=60, 'C6', if( age >=50, 'C5', if( age >=40, 'C4', 'C3' ) ) )
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-17 11:07:08 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองทำแล้วค่ะ มันขึ้น ว่า 
 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (SELECT count( * ) AS age_count, group_concat( nm ) AS NAMES , curre' at line 9
 
 ช่วยทีนะค่ะ ตอนนี้ทำโปคเจคจบอยู่ ติดแค่ส่วนนี้ทำนั้น โปคเจคก็จะเสร็จแล้ว
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-18 11:40:52 | By :
                            chumpoo |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | copy โค้ดที่เอาไป run มาให้ดูหน่อยครับ 
 แล้วก็ตรวจสอบเวอร์ชั่นของ mysql โดยใช้ query
 
 SELECT version( ) ;
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-18 14:07:32 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เวอร์ชั่น mysql ไม่ทราบว่าเป็น เวอร์ชั่น ไหนค่ะ ใช้โปรแกรมที่ใช้ทดสอบ AppServ2.5.10 ค่ะ 
 <?
 $host="localhost";
 $user="root";
 $pass="root";
 $dbname1 = "age";
 
 $connect=mysql_connect($host, $user, $pass) or die(mysql_error());
 mysql_select_db($dbname1) or die(mysql_error());
 
 
 
 $sql="select count(*) as age_count, group_concat(nm) as names, current_date, bd,
 
 year(current_date)-year(bd) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) as age
 
 from user_profiles
 
 group by
 
 year(current_date)-year(bd) + if(date_format(current_date,'%m%d') > date_format(bd,'%m%d'),1,0) ";
 
 
 
 $sql1="SELECT
 
 group_concat( NAMES ) AS age_range_names,
 
 sum( age_count ) AS age_range_count,
 
 if( age >=60, 'more than 60', if( age >=50, '50-59', if( age >=40, '40-49', 'less than 40' ) ) ) AS
 FROM
 (
 SELECT
 count( * ) AS age_count,
 group_concat( nm ) AS NAMES ,
 current_date,
 bd,
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 ) AS age
 FROM user_profiles
 GROUP BY
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 )
 ) AS user_ages
 GROUP BY if( age >=60, 'C6', if( age >=50, 'C5', if( age >=40, 'C4', 'C3' ) ))";
 
 $res = mysql_query($sql1 )or die(mysql_error());
 $numr = mysql_num_rows($res);
 list($age_range_count)=mysql_fetch_array($res);
 echo $age_range_count;
 ?>
 
 ถ้าช่วยอธิบาย โค้ดให้ด้วย จะเป็นความกรุณามากค่ะ
 ขอบคุณค่ะ
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-19 09:04:36 | By :
                            chumpoo |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | <?
$host="localhost";
$user="root";
$pass="root";
$dbname1 = "age";
$connect=mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($dbname1) or die(mysql_error());
$sql="select count(*) as age_count, group_concat(nm) as names, current_date, bd,
year(current_date)-year(bd) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) as age
from user_profiles
group by
year(current_date)-year(bd) + if(date_format(current_date,'%m%d') > date_format(bd,'%m%d'),1,0) ";
$res = mysql_query($sql )or die(mysql_error());
$numr = mysql_num_rows($res);
while($row = mysql_fetch_assoc($res)){
	echo $row['age_count'],'........',$row['names'],'.......',$row['curent_date'],'.......',$row['bd'],'.......',$row['age'],'<br>';
}
echo '<hr>';
$sql1="SELECT
group_concat( NAMES ) AS age_range_names,
sum( age_count ) AS age_range_count,
if( age >=60, 'more than 60', if( age >=50, '50-59', if( age >=40, '40-49', 'less than 40' ) ) ) AS age_range
FROM
(SELECT
count( * ) AS age_count,
group_concat( nm ) AS NAMES ,
current_date,
bd,
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 ) AS age
FROM user_profiles
GROUP BY
year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 )
) AS user_ages
GROUP BY if( age >=60, 'C6', if( age >=50, 'C5', if( age >=40, 'C4', 'C3' ) ) )
";
$res = mysql_query($sql1 )or die(mysql_error());
$numr = mysql_num_rows($res);
while($row = mysql_fetch_assoc($res)){
	echo $row['age_range_names'],'........',$row['age_range_count'],'.......',$row['age_range'],'<br>';
}
?>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-19 10:01:50 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | <? $host="localhost";
 $user="root";
 $pass="12345";
 $dbname1 = "age";
 
 $connect=mysql_connect($host, $user, $pass) or die(mysql_error());
 mysql_select_db($dbname1) or die(mysql_error());
 
 
 $sql="select count(*) as age_count, group_concat(nm) as names, current_date, bd,
 
 year(current_date)-year(bd) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 1, 0 ) as age
 
 from user_profiles
 
 group by
 
 year(current_date)-year(bd) + if(date_format(current_date,'%m%d') > date_format(bd,'%m%d'),1,0) ";
 $res = mysql_query($sql )or die(mysql_error());
 $numr = mysql_num_rows($res);
 while($row = mysql_fetch_assoc($res)){
 echo $row['age_count'],'........',$row['names'],'.......',$row['curent_date'],'.......',$row['bd'],'.......',$row['age'],'<br>';
 }
 
 
 
 
 echo '<hr>';
 $sql1="SELECT
 
 group_concat( NAMES ) AS age_range_names,
 
 sum( age_count ) AS age_range_count,
 
 if( age >=60, 'more than 60', if( age >=50, '50-59', if( age >=40, '40-49', 'less than 40' ) ) ) AS age_range
 
 FROM
 
 (SELECT
 count( * ) AS age_count,
 group_concat( nm ) AS NAMES ,
 current_date,
 bd,
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 ) AS age
 FROM user_profiles
 GROUP BY
 year( current_date ) - year( bd ) + if( date_format( current_date, '%m%d' ) > date_format( bd, '%m%d' ) , 0, -1 )
 ) AS user_ages
 
 GROUP BY if( age >=60, 'C6', if( age >=50, 'C5', if( age >=40, 'C4', 'C3' ) ) )
 ";
 $res = mysql_query($sql1 )or die(mysql_error());
 $numr = mysql_num_rows($res);
 while($row = mysql_fetch_assoc($res)){
 echo $row['age_range_names'],'........',$row['age_range_count'],'.......',$row['age_range'],'<br>';
 }
 
 ?>
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-19 10:02:11 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบพระคุณ มากๆๆๆๆมายนะค่ะ ทำโปรเจคสำเสร็จแล้วค่ะ
 
 
 Go to : SQL MYSQL DATE_FORMAT
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2010-04-19 11:55:43 | By :
                            chumpoo |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 |