Advertisement
cdsatrian

[mysql] calculate support value

Dec 31st, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2. /*********************************
  3. FILENAME  : psb.php
  4. CREATE BY  : cahya dsn
  5. PURPOSE   : calculate support value
  6. CREATE DATE : 2012-12-31
  7. **********************************
  8. #table creation
  9.  
  10. USE `test`;
  11.  
  12. DROP TABLE IF EXISTS `tbl_psb`;
  13. CREATE TABLE IF NOT EXISTS `tbl_psb` (
  14.  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table tbl_psb',
  15.  `nilai` varchar(2) NOT NULL COMMENT 'nilai',
  16.  `jalur_masuk` varchar(4) NOT NULL COMMENT 'jalur masuk',
  17.  `count` int(11) unsigned NOT NULL DEFAULT 0,
  18.  PRIMARY KEY(`id`)
  19. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data PSB';
  20.  
  21. INSERT INTO `tbl_psb`(`id`,`nilai`,`jalur_masuk`,`count`) VALUES
  22. (NULL,'A2','PSSB',3),
  23. (NULL,'A2','SPMB',1),
  24. (NULL,'A3','PSSB',2),
  25. (NULL,'A3','SPMB',3);
  26. */
  27.  
  28. //database configuration
  29. $dbhost='localhost';
  30. $dbuser='root';
  31. $dbpass='';
  32. $dbname='test';
  33. //database connection
  34. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  35. //query to get organization datas from database
  36. $sql="SELECT nilai,jalur_masuk,count,(count/jml) AS support "
  37.     ."FROM tbl_psb, "
  38.     ."(SELECT nilai AS nilai2,SUM(count) AS jml "
  39.     ."  FROM tbl_psb "
  40.     ."  GROUP BY nilai "
  41.     .") AS t2 "
  42.     ."WHERE count>2 "
  43.     ."AND nilai2=nilai";
  44. $result=$db->query($sql);
  45. echo "<pre>"
  46.     ."Nilai | Jalur Masuk | Count | Support |\n";
  47. while($records=$result->fetch_object()){
  48.   echo  str_pad($records->nilai,6,' ',STR_PAD_BOTH)."|";
  49.   echo  str_pad($records->jalur_masuk,13,' ',STR_PAD_BOTH)."|";
  50.   echo  str_pad($records->count,7,' ',STR_PAD_BOTH)."|";
  51.   echo  str_pad($records->support,9,' ',STR_PAD_BOTH)."|\n";
  52. }
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement