|
 |
|
ทำอย่างไรให้ แสดงข้อมูล GROUP เดียวกัน แต่ ข้อความต่อกัน ภาษา SQL มีรูปภาพประกอบ |
|
 |
|
|
 |
 |
|
นี่ Code ภาษา SQL นะค่ะ
Code (PHP)
SELECT SUM( dt.dt_ustu ) AS UnitStudent, SUM( dt.dt_sstu ) AS SheetStudent, CONCAT( daex.daex_date, '/', daex.daex_mont, '/', daex.daex_year ) AS NewTimeExam, sj.sj_code, sj.sj_name, dt.dt_sec, dt.dt_rname, dt.dt_ustu, dt.dt_sstu, dt.dt_gpstu, daex.daex_date, daex.daex_mont, daex.daex_year, tex.tex_detail
FROM subject sj, datateach dt, timeexam tex, dayexam daex
WHERE dt.dt_sj_id = sj.sj_id
AND daex.daex_id = dt.dt_daex_id
AND tex.tex_id = dt.dt_tex_id
GROUP BY dt.dt_sj_id,sj.sj_code,NewTimeExam
LIMIT 0 , 30

รูปภาพที่ 1
นี่คือ ข้อมูลดิบนะคะ

รูปภาพที่ 2
นี่คือ การ Query โดยใช้ Code ด้านบน นะค่ะ

รูปภาพที่ 3
นี่คือ Code ที่ใช้ในการ Query ค่ะ
สิ่งที่ต้องการให้มันเปลี่ยนคือ

รูปภาพที่ 4
เป็นแบบนี้นะค่ะ
ตรงตารางใหม่ ที่ชื่อ Newsec หนูใช้ CONCAT('อธิบาย') AS NewSec นะค่ะ ดูตรง dt_id 1,2 มันจะคล้ายๆ กัน พอ GROUP ออกมามันจะได้ แบบ รูปภาพที่2 แต่ อยากให้ dt_sec มันเรียง กันอะค่ะ ตรง NewSec มันจะได้ 4,3 และอีกบรรทัด กะเป็น 6,5
ตามรูปภาพนี้นะคะ ตัดต่อเอา อิอิทำไม่เป็น

รูปภาพที่ 5
ต้องการให้แสดง ข้อมูล เหมือนรูปภาพที่ 5 ค่ะ
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2011-12-26 01:00:52
|
 |
 |
 |
 |
Date :
2011-12-25 12:05:03 |
By :
เจ้าปัญหา |
View :
1233 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่านหลายรอบยังไม่เข้าใจว่าต้องการจะทำอะไร เลยไม่มีใครตอบครับ
|
 |
 |
 |
 |
Date :
2011-12-25 23:23:20 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษนะคะ ไม่รู้ว่าจะอธิบายยังไงดีเหมือนกัน ดูตรงนี้ใหม่ อีกรอบนะคะ
นี่เป็นข้อมูลที่ยังไม่ได้ทำการ GROUP แต่อย่างใดนะคะ

ตรงนี้ยกตัวอย่าง ข้อมูลมี 4 ตัว โดยจะทำการ GROUP dt_sj_id , dt_sec, dt_daex_id , dt_tex_id ตามที่วงกลมไว้นะคะ วงกลม คือ ข้อมูลที่เหมือนกัน ค่ะ ตรง dt_sec มีสี่เ่หลี่ยม สีเขียว คือข้อมูลที่แตกต่างกัน คือ เลข 6 กับเลข 5 นี่คือใน GROUP ที่ 1 นะคะ ส่วนใน GROUP ที่ 2 ก็เป็นลักษณะเดียวกัน เป็นสีเหลี่ยม สีเขียว คะ แต่แตกต่างกันตรงที่ dt_sec เป็นเลข 4 กับ 3
ต่อไปหนูจะทำการ GROUP BY นะคะ
GROUP BY dt_sj_id, dt_daex_id , dt_tex_id ก็จะได้ตามรูปนี้

ข้อสังเกต dt_id 2 กับ dt_id 4 หายไป เนื่องจาก ทำการ GROUP กัน เหลือแต่ dt_id 1 และ dt_id 3
สิ่งที่อยากให้ทำก็คือ หรือช่วยกันคิดหนอ่ยนะคะว่า dt_id 2 และ dt_id 4 หายไปไหน แล้วจะนำข้อมูลของ dt_id 2 มาจัดในรูป GROUP ด้วยได้หรือไม่ dt_id 4 ก็เช่นเดียวกันคะ
อยากให้เพื่อนๆพี่ๆช่วยให้ผลลัพท์ออกเป็นแบบนี้นะคะ

ภาพนี้ สังเกต ตรง ที่หนูวงกลมไว้ ต่างกับภาพ ด้านบนตรงที่ ตรงวงกลมสีแดงนะคะ จะมีตัว , และ เลข 5 เพิ่มเข้ามาในช่อง dt_sec เลข 5 ได้มาจากที่ไหน ได้มาจาก ข้อมูลของ dt_id 2 ที่มันหายไปนั่นแหล่ะคะ และวงกลมเขียวก็เช่นกัน จะมีตัว , และ เลข 3 เพิ่มเข้ามา ในช่อง dt_sec นะคะ เลข 3 ได้มาจาก dt_id 4 คะ ที่มันหายไปจากการ GROUP
****ต้องเขียน CODE ยังไงคะ ที่จะให้ มีตัว , และ ตัวที่ GROUP หายไปมาใส่ แทนต่อไปเรื่อยๆ เช่น 4,3,5,15,16,18,12 ตาม GROUP ไปเรื่อยๆ อะคะ
ถ้าไม่เข้าใจยังไง คือ อธิบายไม่ค่อยเก่งนะคะ ช่วย comment กลับให้หนอ่ยนะคะ ว่าไม่เข้าใจตรงไหน ขอบคุณมากค่ะ
|
 |
 |
 |
 |
Date :
2011-12-26 01:50:57 |
By :
เจ้าปัญหา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สร้าง table ตามนี้ แล้วรันดูครับ
Quote:DROP TABLE IF EXISTS `mytbl`;
CREATE TABLE IF NOT EXISTS `mytbl` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id2` int(10) DEFAULT NULL,
`id3` int(10) DEFAULT NULL,
`id4` int(10) DEFAULT NULL,
`desc` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `mytbl` (`id`, `id2`, `id3`, `id4`, `desc`) VALUES
(1, 4, 7, 2, '6'),
(2, 4, 7, 2, '5'),
(3, 3, 7, 6, '4'),
(4, 3, 7, 6, '3');
Quote:select group_concat(id SEPARATOR ',') as id1,id2,id3,id4,group_concat(`desc` SEPARATOR ',') as `desc` from mytbl group by id2,id3,id4
|
 |
 |
 |
 |
Date :
2011-12-26 02:06:16 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะ ใช้งานได้แล้ว นำ code ตัวอย่างที่พี่ให้ดัดแปลงเอา อิอิ Plus1
|
 |
 |
 |
 |
Date :
2011-12-26 12:38:32 |
By :
เจ้าปัญหา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|