Advertisement
cdsatrian

Jumlah Buku & Peminjam Terbanyak tiap Bulan

Nov 29th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.49 KB | None | 0 0
  1. USE `test`;
  2.  
  3. DROP TABLE IF EXISTS `tbl_anggota`;
  4. CREATE TABLE IF NOT EXISTS `tbl_anggota` (
  5.   `id_anggota` int(11) unsigned NOT NULL AUTO_INCREMENT,
  6.   `nama` VARCHAR(30) NOT NULL,
  7.   PRIMARY KEY (`id_anggota`)
  8. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  9.  
  10. INSERT INTO `tbl_anggota`
  11. (`nama`)
  12. VALUES
  13. ('Agus Pambudi'),
  14. ('Budiati Wijayanti'),
  15. ('Carla Hapsari'),
  16. ('Dedi Suhendra');
  17.  
  18. DROP TABLE IF EXISTS `tbl_buku`;
  19. CREATE TABLE IF NOT EXISTS `tbl_buku` (
  20.   `id_buku` int(11) unsigned NOT NULL AUTO_INCREMENT,
  21.   `judul` VARCHAR(50) NOT NULL,
  22.   PRIMARY KEY (`id_buku`)
  23. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  24.  
  25. INSERT INTO `tbl_buku`
  26. (`judul`)
  27. VALUES
  28. ('Pemrograman PHP dan MySQL'),
  29. ('Administrasi Database MySQL'),
  30. ('Pemrograman PHP untuk Pemula'),
  31. ('PHP dan AJaX');
  32.  
  33.  
  34. DROP TABLE IF EXISTS `tbl_pinjam`;
  35. CREATE TABLE IF NOT EXISTS `tbl_pinjam` (
  36.   `id_pinjam` int(11) unsigned NOT NULL AUTO_INCREMENT,
  37.   `tgl_pinjam` DATE NOT NULL,
  38.   `tgl_kembali` DATE NOT NULL DEFAULT 0,
  39.   `id_buku` smallint(5) unsigned NOT NULL,
  40.   `id_anggota` smallint(5) unsigned NOT NULL,
  41.   PRIMARY KEY (`id_pinjam`)
  42. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  43.  
  44. INSERT INTO `tbl_pinjam`
  45. (`tgl_pinjam`,`id_buku`,`id_anggota`)
  46. VALUES
  47. ('2013-11-22',2,4),
  48. ('2013-11-24',2,1),
  49. ('2013-11-24',3,1),
  50. ('2013-11-25',1,2),
  51. ('2013-11-28',2,3),
  52. ('2013-11-30',3,2),
  53. ('2013-12-01',2,4),
  54. ('2013-12-02',4,4),
  55. ('2013-12-05',4,1);
  56.  
  57. // menampilkan jumlah peminjaman buku berdasarkan judul buku
  58. SELECT b.judul,COUNT(a.id_buku) AS jml, MONTH(a.tgl_pinjam) AS bln
  59. FROM
  60. tbl_pinjam a
  61. JOIN tbl_buku b USING(id_buku)
  62. GROUP BY a.id_buku,bln
  63. ORDER BY bln;
  64.  
  65. // menampilkan jumlah peminjaman buku terbanyak tiap bulan berdasarkan judul buku
  66. SELECT c.judul,MAX(c.jml) AS jml,c.bln
  67. FROM
  68. (
  69. SELECT b.judul,COUNT(a.id_buku) AS jml, MONTH(a.tgl_pinjam) AS bln
  70. FROM
  71. tbl_pinjam a
  72. JOIN tbl_buku b USING(id_buku)
  73. GROUP BY a.id_buku,bln
  74. ORDER BY bln,jml DESC
  75. )c
  76. GROUP BY c.bln;
  77.  
  78. // menampilkan jumlah peminjaman buku berdasarkan anggota
  79. SELECT b.nama,COUNT(a.id_anggota) AS jml, MONTH(a.tgl_pinjam) AS bln
  80. FROM
  81. tbl_pinjam a
  82. JOIN tbl_anggota b USING(id_anggota)
  83. GROUP BY a.id_anggota,bln
  84. ORDER BY bln,b.nama;
  85.  
  86. // menampilkan jumlah peminjaman buku terbanyak tiap bulan berdasarkan anggota
  87. SELECT c.nama,MAX(c.jml) AS jml,c.bln
  88. FROM
  89. (
  90. SELECT b.nama,COUNT(a.id_anggota) AS jml, MONTH(a.tgl_pinjam) AS bln
  91. FROM
  92. tbl_pinjam a
  93. JOIN tbl_anggota b USING(id_anggota)
  94. GROUP BY a.id_anggota,bln
  95. ORDER BY bln,jml DESC
  96. )c
  97. GROUP BY c.bln;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement