Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > PHP > PHP Forum > กูรู ช่วยหน่อยค่ะ อยากทราบการเขียนโค้ด แบ่งช่วงอายุ


กูรู ช่วยหน่อยค่ะ อยากทราบการเขียนโค้ด แบ่งช่วงอายุ

 
Topic : 041598



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์



ต้องการทราบจำนวนสมาชิก โดยแบ่งตามช่วงอายุ ค่ะ
คือข้อมูลวันเกิดของสมาชิก ในฐานข้อมูล ซึ่งเก็บค่าเป็น Date (2010-04-10) มาคำนวณเป็นอายุ และเมื่อได้ข้อมูลอายุแล้ว นำมาแบ่งเป็น ช่วงอายุ เช่น ช่วงอายุ15-20ปี มีจำนวน XXX คน




Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-04-16 21:03:26 By : chumpoo View : 4350 Reply : 12
 

 

No. 1



โพสกระทู้ ( 114 )
บทความ ( 0 )



สถานะออฟไลน์


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
 

 

No. 2

Guest


สร้างตารางแล้วลอง 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
 

 

No. 3

Guest


ลืมให้ข้อมูลทดสอบครับ

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
 

 

No. 4

Guest


เอิ๊ก ๆ
ผมว่าแล้ว ว่าทำไมเว็บเก่าคุณหนุ่มเข้าไม่ได้
ที่แท้ ย้ายโฮสต์ โอเค ผมได้ที่เข้าไปสูบความรู้แล้ว อิอิ

ยินดีด้วยครับคุณหนุ่ม
Date : 2010-04-17 08:27:37 By : deawx
 

 

No. 5

Guest


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
 

 

No. 6



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


ลองทำแล้วค่ะ มันขึ้น ว่า

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
 

 

No. 7

Guest


copy โค้ดที่เอาไป run มาให้ดูหน่อยครับ

แล้วก็ตรวจสอบเวอร์ชั่นของ mysql โดยใช้ query

SELECT version( ) ;
Date : 2010-04-18 14:07:32 By : num
 

 

No. 8



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


เวอร์ชั่น 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
 

 

No. 9

Guest


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

 

No. 10

Guest


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

 

No. 11



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบพระคุณ มากๆๆๆๆมายนะค่ะ
ทำโปรเจคสำเสร็จแล้วค่ะ


Go to : SQL MYSQL DATE_FORMAT
Date : 2010-04-19 11:55:43 By : chumpoo
 

 

No. 12

Guest


mysql age range calculate
Date : 2010-10-18 17:21:00 By : num
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : กูรู ช่วยหน่อยค่ะ อยากทราบการเขียนโค้ด แบ่งช่วงอายุ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่