Advertisement
abysalim

Untitled

Nov 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #kasus joining table visits, patients, villages
  2. #penggunaan select * , select by field yang ditentukan
  3.  
  4. SELECT * FROM
  5. `visits` V, patients P,
  6. villages Vl
  7.  
  8. WHERE
  9.  
  10. V.patient_id = P.id
  11. AND V.village_id = Vl.id
  12.  
  13. Showing rows 0 - 24 (352759 total, Query took 0.1554 seconds.)
  14.  
  15.  
  16. SELECT V.nama,V.nama_kk,Vl.desa,P.alamat
  17. FROM
  18. `visits` V, patients P,
  19. villages Vl
  20.  
  21. WHERE
  22.  
  23. V.patient_id = P.id
  24. AND V.village_id = Vl.id
  25.  
  26. Showing rows 0 - 24 (352759 total, Query took 0.0036 seconds.)
  27.  
  28.  
  29. SELECT * FROM `visits` V inner join patients P on V.patient_id=P.id inner join villages Vl on Vl.id = V.village_id
  30.  
  31. Showing rows 0 - 24 (352759 total, Query took 0.0031 seconds.)
  32.  
  33.  
  34. -------------------------------------------------------------------------
  35.  
  36.  
  37. # kasus joining table visits -> table anamnesis
  38.  
  39. select * from visits V, anamnesis An
  40. where V.id=An.visit_id
  41.  
  42. Showing rows 0 - 24 (47943 total, Query took 0.0946 seconds.)
  43.  
  44.  
  45. select * from visits V
  46. left join anamnesis An on V.id=An.visit_id
  47.  
  48. Showing rows 0 - 24 (362217 total, Query took 0.0996 seconds.)
  49.  
  50.  
  51. select * from visits V
  52. inner join anamnesis An on V.id=An.visit_id
  53.  
  54. Showing rows 0 - 24 (47943 total, Query took 0.0027 seconds.)
  55.  
  56.  
  57. ------------------------------------------------------------------------
  58.  
  59. sample relasional 5 table
  60.  
  61. #select custom field from 5 related table (use join)
  62.  
  63. select V.nama, V.tanggal,V.nama_kk from visits V inner join anamnesis An on V.id=An.visit_id inner join patients P on V.patient_id=P.id inner join villages Vl on V.village_id=Vl.id inner join orchards Orc on V.orchard_id=Orc.id
  64.  
  65.  
  66. Showing rows 0 - 24 (33849 total, Query took 0.0055 seconds.)
  67.  
  68.  
  69. #select all field from 5 related table (use join)
  70.  
  71. select * from visits V inner join anamnesis An on V.id=An.visit_id inner join patients P on V.patient_id=P.id inner join villages Vl on V.village_id=Vl.id inner join orchards Orc on V.orchard_id=Orc.id
  72.  
  73.  
  74. Showing rows 0 - 24 (33849 total, Query took 0.0058 seconds.)
  75.  
  76. ---------------------------
  77.  
  78. #select custom field from 5 related table
  79.  
  80. select V.tanggal, P.nama, Vl.desa, Orc.dusun
  81. from visits V, anamnesis An, patients P, villages Vl, orchards Orc
  82. where
  83. V.id=An.visit_id
  84. AND V.patient_id=P.id
  85. AND Vl.id = V.village_id
  86. AND V.orchard_id = Orc.id
  87.  
  88. Showing rows 0 - 24 (33849 total, Query took 0.0055 seconds.)
  89.  
  90.  
  91.  
  92. #select all field from 5 related table
  93.  
  94. select *
  95. from visits V, anamnesis An, patients P, villages Vl, orchards Orc
  96. where
  97. V.id=An.visit_id
  98. AND V.patient_id=P.id
  99. AND Vl.id = V.village_id
  100. AND V.orchard_id = Orc.id
  101.  
  102. Showing rows 0 - 24 (33849 total, Query took 0.0047 seconds.)
  103.  
  104.  
  105. -------------------------------------------------------------------
  106.  
  107.  
  108. where / having
  109.  
  110. #menggunakan WHERE
  111.  
  112. select *
  113. from visits V, anamnesis An, patients P, villages Vl, orchards Orc
  114. where
  115. V.id=An.visit_id
  116. AND V.patient_id=P.id
  117. AND Vl.id = V.village_id
  118. AND V.orchard_id = Orc.id
  119. AND V.patient_id = '03020001'
  120.  
  121. Showing rows 0 - 0 (1 total, Query took 0.0786 seconds.)
  122.  
  123.  
  124. select V.nama,V.nama_kk
  125. from visits V, anamnesis An, patients P, villages Vl, orchards Orc
  126. where
  127. V.id=An.visit_id
  128. AND V.patient_id=P.id
  129. AND Vl.id = V.village_id
  130. AND V.orchard_id = Orc.id
  131. AND V.patient_id = '03020001'
  132.  
  133.  
  134. Showing rows 0 - 0 (1 total, Query took 0.0055 seconds.)
  135.  
  136.  
  137. #menggunakan HAVING
  138.  
  139. select *
  140. from visits V, anamnesis An, patients P, villages Vl, orchards Orc
  141. where
  142. V.id=An.visit_id
  143. AND V.patient_id=P.id
  144. AND Vl.id = V.village_id
  145. AND V.orchard_id = Orc.id
  146.  
  147. having V.patient_id = '03020001'
  148.  
  149. Showing rows 0 - 0 (1 total, Query took 0.3835 seconds.)
  150.  
  151.  
  152. select V.nama,V.nama_kk, V.patient_id
  153. from
  154. visits V, anamnesis An, patients P, villages Vl, orchards Orc
  155. where
  156. V.id=An.visit_id AND V.patient_id=P.id AND Vl.id = V.village_id AND V.orchard_id = Orc.id having V.patient_id = '03020001'
  157.  
  158. having V.patient_id = '03020001'
  159.  
  160. Showing rows 0 - 0 (1 total, Query took 0.2444 seconds.)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement