AKZN Notes

Archives for My Lazy and Forgetful Mind

RANDOM DATA Table Order

Last Modified on

[MYSQL][PROCEDURE] RANDOM DATA Table Order

tb_order


DELIMITER $$
DROP PROCEDURE IF EXISTS make_order$$
CREATE PROCEDURE make_order ()
BEGIN
  DECLARE monthcounts INT DEFAULT 1;
  DECLARE daycounts INT DEFAULT 1;
  DECLARE orderbydaycounts INT DEFAULT 1;
  WHILE
    monthcounts < 9 DO SET daycounts = 1;
    WHILE
      daycounts < 31 DO SET orderbydaycounts = 1;
      WHILE
        orderbydaycounts < 5 DO
        INSERT INTO `tb_order_copy` (
          kode_order,
          tgl_order,
          nama_penerima,
          id_provinsi,
          kota,
          kodepos,
          no_telp,
          ongkir
        )
        VALUES
          (
            SUBSTRING(MD5 (RAND ()), 1, 5),
            CONCAT ('2020-06-', daycounts),
            CONCAT (
              generate_fname (),
              ' ',
              generate_lname ()
            ),
            1,
            'yogyakarta',
            '55198',
            CONCAT (
              '0858',
              LPAD (FLOOR (RAND () * 999999.99), 6, '0')
            ),
            12000
          );
        SET orderbydaycounts = orderbydaycounts + 1;
      END WHILE;
      SET daycounts = daycounts + 1;
    END WHILE;
    SET monthcounts = monthcounts + 1;
  END WHILE;
END $$

DELIMITER ;  

tb_order_detail

DROP PROCEDURE IF EXISTS insertOrderDetail;
DELIMITER ;;

CREATE PROCEDURE insertOrderDetail()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE LIMITVAR INT DEFAULT 0;
DECLARE JUMLAH INT DEFAULT 0;
DECLARE IDORDER INT DEFAULT 0;
SELECT COUNT(id_order) FROM tb_order_copy INTO n;
SET i=0;
WHILE i<n DO 
  # INSERT INTO tb_order_detail_copy(ID, VAL) SELECT (ID, VAL) FROM tb_order_copy LIMIT i,1;
  SELECT (id_order) FROM tb_order_copy LIMIT i,1 into IDORDER;

case  when i % 2 <> 0 then  set LIMITVAR = 1; ELSE set LIMITVAR = 2; end case;

set JUMLAH =(FLOOR( 1 + RAND( ) *2 ));

INSERT INTO tb_order_detail_copy(id_order,id_produk, harga, jumlah,subtotal) SELECT IDORDER, t1.`id_produk`,harga,JUMLAH,(jumlah*harga) FROM tb_produk AS t1 JOIN (SELECT id_produk FROM tb_produk ORDER BY RAND() limit LIMITVAR ) AS t2 ON t1.id_produk=t2.id_produk;

  SET i = i + 1;
END WHILE;
End;
;;

Leave a Reply

Your email address will not be published.