Advertisement
cdsatrian

mysql booking room

Nov 22nd, 2023
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.31 KB | Source Code | 0 0
  1. DROP TABLE IF EXISTS sesi;
  2. CREATE TABLE sesi(
  3.     idsesi int2,
  4.     namasesi VARCHAR(50),
  5.     PRIMARY KEY (idsesi)
  6. );
  7. INSERT INTO sesi(idsesi,namasesi)
  8. VALUES
  9.     (1,'08:00-09:00'),
  10.     (2,'09:00-10:00'),
  11.     (3,'10:00-11:00'),
  12.     (4,'11:00-12:00'),
  13.     (5,'13:00-14:00'),
  14.     (6,'14:00-15:00'),
  15.     (7,'15:00-16:00'),
  16.     (8,'16:00-17:00')
  17. ;
  18.  
  19. DROP TABLE IF EXISTS ruangan;
  20. CREATE TABLE ruangan(
  21.     idruangan int2,
  22.     namaruangan VARCHAR(50),
  23.     PRIMARY KEY (idruangan)
  24. );
  25.  
  26. INSERT INTO ruangan(idruangan,namaruangan)
  27. VALUES
  28.     (1,'101'),
  29.     (2,'102'),
  30.     (3,'201'),
  31.     (4,'202'),
  32.     (5,'301'),
  33.     (6,'302')
  34. ;
  35.  
  36. DROP TABLE IF EXISTS USER;
  37. CREATE TABLE USER(
  38.     iduser int2,
  39.     namauser VARCHAR(50),
  40.     PRIMARY KEY (iduser)
  41. );
  42.  
  43. INSERT INTO USER(iduser,namauser)
  44. VALUES
  45.     (1,'Andi'),
  46.     (2,'Budi'),
  47.     (3,'Carla'),
  48.     (4,'Dewi'),
  49.     (5,'Edo'),
  50.     (6,'Farah')
  51. ;
  52.  
  53. DROP TABLE IF EXISTS seminar;
  54. CREATE TABLE seminar(
  55.     idseminar int2,
  56.     idsesi int2,
  57.     iduser int2,
  58.     idruangan int2,
  59.     tanggal DATE,
  60.     PRIMARY KEY (idseminar)
  61. );
  62.  
  63. INSERT INTO seminar(idseminar,idsesi,iduser,idruangan,tanggal)
  64. VALUES
  65.     (1,2,1,2,'2023-11-20'),
  66.     (2,3,2,1,'2023-11-20'),
  67.     (3,5,3,4,'2023-11-21'),
  68.     (4,5,4,3,'2023-11-21'),
  69.     (5,6,5,4,'2023-11-21'),
  70.     (6,6,6,2,'2023-11-21')
  71. ;
  72. -- status ruangan '202' pada tanggal '2023-11-21'
  73. SELECT
  74.     s.namasesi,
  75.     s2.namauser,
  76.     CASE
  77.         WHEN s2.namauser IS NULL THEN
  78.             'tersedia'
  79.         ELSE
  80.             'booked'
  81.     END AS statusruangan
  82. FROM
  83.     (
  84.     SELECT
  85.         s.tanggal,
  86.         r.namaruangan,
  87.         u.namauser,
  88.         s.idsesi
  89.     FROM seminar s
  90.         JOIN ruangan r
  91.             ON r.idruangan=s.idruangan
  92.                 AND r.namaruangan='202'
  93.         JOIN USER u
  94.             ON s.iduser=u.iduser
  95.     ) s2
  96.     RIGHT JOIN sesi s
  97.         ON s2.idsesi=s.idsesi
  98.             AND s2.tanggal='2023-11-21'
  99. ORDER BY
  100.     s.namasesi
  101. ;
  102.  
  103. +-------------+----------+---------------+
  104. | namasesi    | namauser | statusruangan |
  105. +-------------+----------+---------------+
  106. | 08:00-09:00 | NULL     | tersedia      |
  107. | 09:00-10:00 | NULL     | tersedia      |
  108. | 10:00-11:00 | NULL     | tersedia      |
  109. | 11:00-12:00 | NULL     | tersedia      |
  110. | 13:00-14:00 | Carla    | booked        |
  111. | 14:00-15:00 | Edo      | booked        |
  112. | 15:00-16:00 | NULL     | tersedia      |
  113. | 16:00-17:00 | NULL     | tersedia      |
  114. +-------------+----------+---------------+
  115. 8 ROWS IN SET (0.01 sec)
  116.  
  117. -- status ruangan '101' pada tanggal '2023-11-21'
  118. SELECT
  119.     s.namasesi,
  120.     s2.namauser,
  121.     CASE
  122.         WHEN s2.namauser IS NULL THEN
  123.             'tersedia'
  124.         ELSE
  125.             'booked'
  126.     END AS statusruangan
  127. FROM
  128.     (
  129.     SELECT
  130.         s.tanggal,
  131.         r.namaruangan,
  132.         u.namauser,
  133.         s.idsesi
  134.     FROM seminar s
  135.         JOIN ruangan r
  136.             ON r.idruangan=s.idruangan
  137.                 AND r.namaruangan='101'
  138.         JOIN USER u
  139.             ON s.iduser=u.iduser
  140.     ) s2
  141.     RIGHT JOIN sesi s
  142.         ON s2.idsesi=s.idsesi
  143.             AND s2.tanggal='2023-11-21'
  144. ORDER BY
  145.     s.namasesi
  146. ;
  147.  
  148. +-------------+----------+---------------+
  149. | namasesi    | namauser | statusruangan |
  150. +-------------+----------+---------------+
  151. | 08:00-09:00 | NULL     | tersedia      |
  152. | 09:00-10:00 | NULL     | tersedia      |
  153. | 10:00-11:00 | NULL     | tersedia      |
  154. | 11:00-12:00 | NULL     | tersedia      |
  155. | 13:00-14:00 | NULL     | tersedia      |
  156. | 14:00-15:00 | NULL     | tersedia      |
  157. | 15:00-16:00 | NULL     | tersedia      |
  158. | 16:00-17:00 | NULL     | tersedia      |
  159. +-------------+----------+---------------+
  160. 8 ROWS IN SET (0.63 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement