Read 188 times | Created 2014-04-22 06:14:38 | Updated 2014-04-22 06:14:38 | | |

 

-- -
USE `test`;

DROP TABLE IF EXISTS `buku`;
CREATE TABLE IF NOT EXISTS `buku` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `nama_buku` varchar(30) NOT NULL,
 `jenis_id` int(11) unsigned NOT NULL,
 PRIMARY KEY(`id`)
) ENGINE=MyISAM;

INSERT INTO `buku`
VALUES(1,'Habis Gelap TT',1),
(2,'Mataharimu',2),
(3,'Selayang pandang',3);

DROP TABLE IF EXISTS `jenisbuku`;
CREATE TABLE IF NOT EXISTS `jenisbuku` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `jenisbuku` varchar(30) NOT NULL,
 PRIMARY KEY(`id`)
) ENGINE=MyISAM;

INSERT INTO `jenisbuku`
VALUES(1,'Komedi'),
(2,'Ilmiah'),
(3,'seni');

SELECT
GROUP_CONCAT(IF(b.id=1,a.nama_buku,NULL)) AS komedi,
GROUP_CONCAT(IF(b.id=2,a.nama_buku,NULL)) AS ilmiah,
GROUP_CONCAT(IF(b.id=3,a.nama_buku,NULL)) AS seni
FROM buku a
JOIN jenisbuku b ON b.id=a.jenis_id;
-- -