看,我把它作为mySQL中的数据。表名是" schedule"
=============================================== =======
Mapel | Kelas | Tanggal |那抹|状态
=============================================== =======
BHS。印度|| 1A || 2015-01-14 ||阿迪|| x
BHS。印度|| 1A || 2015-01-14 || Ani || x
BHS。印度|| 1A || 2015-01-14 ||思思|| -
BHS。印度|| 1A || 2015-01-14 ||塔拉|| -
BHS。印度|| 1A || 2015-01-14 || Wina || x
数学|| 1A || 2015-01-15 ||阿迪|| x
数学|| 1A || 2015-01-15 || Ani || -
数学|| 1A || 2015-01-15 ||思思|| -
数学|| 1A || 2015-01-15 ||塔拉|| -
数学|| 1A || 2015-01-15 || Wina || x
BHS。印度|| 1A || 2015-01-15 ||阿迪|| x
BHS。印度|| 1A || 2015-01-15 || Ani || -
BHS。印度|| 1A || 2015-01-15 ||思思|| -
BHS。印度|| 1A || 2015-01-15 ||塔拉|| -
BHS。印度|| 1A || 2015-01-15 || Wina || x
现在我想制作一个表格依赖于" MAPEL"和#34; KELAS" 如果我选择Mapel =' Bhs。印度'和KELAS =' 1A'会显示这样的表
=============================================== =======
Kelas | Nama | 2015-01-14 | 2015-01-15 |
=============================================== =======
1A ||阿迪|| x || X
1A || Ani || x || -
1A ||思思|| - || -
1A || Tara || - || -
1A || Wina || x || X
答案 0 :(得分:0)
如果你想根据表中的实际内容动态获取日期,那么唯一可行的方法就是使用动态SQL
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT(
'MAX(CASE WHEN tanggal = ''', tanggal,
''' THEN status END) `', tanggal, '`'))
INTO @sql
FROM schedule
WHERE mape = 'Bhs. Indo'
AND kelas = '1A'
ORDER BY tanggal;
SET @sql = CONCAT(
'SELECT kelas, nama, ', @sql,
' FROM schedule ',
' WHERE mape = ''Bhs. Indo''',
' AND kelas = ''1A''',
' GROUP BY kelas, nama'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
输出:
| KELAS | NAMA | 2015-01-14 | 2015-01-15 | |-------|------|------------|------------| | 1A | Adi | x | x | | 1A | Ani | x | - | | 1A | Sisi | - | - | | 1A | Tara | - | - | | 1A | Wina | x | x |
这是 SQLFiddle 演示
您可以将其包装在存储过程中,以简化客户端代码中的操作。