 |
ถามหน่อยนะค่ะ อยากดึงข้อมูลคะแนนล่าสุดในแต่ละบทเรียนของแต่ละคนออกมา จะทำยังไงค่ะ GROUP BY แล้ว |
|
 |
|
|
 |
 |
|
ถามหน่อยนะค่ะ อยากดึงข้อมูลคะแนนล่าสุดในแต่ละบทเรียนของแต่ละคนออกมา จะทำยังไงค่ะ GROUP BY และเรียงข้อมูล โดยใช้ order by แต่ผลลัพธ์กลับได้ข้อมูลเก่าสุดของแต่ละบทเรียนของแต่ละคนออกมาแทนค่ะ ลองค้นในเน็ทแล้วยังทำไม่ได้เลยค่ะ
อยากได้เหมือนในภาพค่ะ

ซึ่งมีตาราง user,chapter(บทเรียน),score(ตารางเก็บคะแนน),term,year

แต่พอเขียนโค้ด
select score.score_id,score.date,score.time,score.username,user.fname,user.lname,chapter.chapter,score.value,min(value) as min ,max(value) as max,avg(value) as avg ,year.year,term.term from score,chapter,user,term,year where score.username=user.username AND score.chapter_id=chapter.chapter_id and score.year_id=year.year_id and score.term_id=term.term_id GROUP BY score.username, chapter.chapter_id ORDER BY score.score_id DESC
กลับได้ข้อมูลเก่าสุดของคะแนนแต่ละบทเรียนของนักเรียนแต่ละคนค่ะ

แก้โค้ดอย่างไรค่ะ ขอบคุณค่ะ
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2010-08-19 00:32:57 |
By :
kittykampong |
View :
2453 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
order by date desc
รึเปล่าครับ ไม่แน่ใจ
|
 |
 |
 |
 |
Date :
2010-08-19 04:34:54 |
By :
blacklion |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สงสัยจะต้องใช้ SUB SELECT ครับ
โดย where term_id = (SELECT MAX(term_ID) from table_score WHERE user_id='xxxx')
|
 |
 |
 |
 |
Date :
2010-08-19 06:19:48 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะ
ได้แล้วค่ะ แต่ติดปัญหาและข้อคงใจนิดหน่อยค่ะ
หนูลองใช้ SUB SELECT ดูค่ะ(ตามที่พี่แนะนำ แต่ใช้ order by date desc ไม่ได้ค่ะมันจะเรียงจากวันที่มากมาน้อย)
แบบนี้
select score.score_id,score.date,score.time,score.username,user.fname,user.lname,chapter.chapter,score.value,min(value) as min ,max(value) as max,avg(value) as avg ,year.year,term.term
from score,chapter,user,term,year
where score.score_id = (SELECT MAX(score.score_id) from score where score.username=user.username AND score.chapter_id=chapter.chapter_id and score.year_id=year.year_id and score.term_id=term.term_id GROUP BY score.username, chapter.chapter_id) GROUP BY score.username, chapter.chapter_id
คอนแรก score_id = 8 มันหายไป หนูเลยลองเข้าไปเปลี่ยนข้อมูลใน ให้ term_id จาก 2 เป็น 1 แล้วคิวรี่ใหม่ปรากฎว่าข้อมูลที่ได้ถูกต้องตามที่ต้องการ แต่ทำใมไม่เข้าใจค่ะว่า term_id = 2 แล้วไม่ได้ค่ะ
|
 |
 |
 |
 |
Date :
2010-08-20 06:09:00 |
By :
kittykampong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|