Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1.  
  2. use cia;
  3.  
  4. /*Oppgave 1*/
  5.  
  6. /*Velg ut kolonner:*/
  7. select * from cia;
  8. select name, region, gdp from cia;
  9. select name, region, area, population from cia;
  10. select name, population/area from cia;
  11. select name, population/1000000 fom cia;
  12.  
  13. /*Velg ut rader:*/
  14. select * from cia where region = "Europe";
  15. select name from cia where population = 0;
  16. select name from cia where population >15000000;
  17. select name from cia where region = "Europe" AND population >15000000;
  18. select name from cia where region = "Europe" OR region = "Asia";
  19.  
  20. /*Velg ut etter mønstre i visse kolonner:*/
  21. select name from cia where name LIKE "b%";
  22. select name from cia where name LIKE "__d%";
  23. select name from cia where length(name) = 4;
  24. select name from cia where name LIKE "%United%";
  25. select name from cia where name LIKE "%ia";
  26.  
  27. /*Sortering:*/
  28. select name from cia order by name;
  29. select name from cia order by region,name;
  30. select name from cia where population >100000000 order by population;
  31. select name, population from cia order by region, population desc;
  32.  
  33. /*Aggegering:*/
  34. select COUNT(name) from cia;
  35. select COUNT(name) from cia where region="Europe";
  36. select SUM(population) from cia where region="Europe";
  37. select region, AVG(population) from cia GROUP BY region;
  38. select COUNT(name) from cia where region="Asia";
  39. select SUM(AREA) from cia where population = 0;
  40. select MAX(population) from cia;
  41.  
  42. /*Gruppering:*/
  43. select region, SUM(area) from cia GROUP BY region;
  44. select region, AVG(area) from cia GROUP BY region;
  45. select region, MAX(area) from cia GROUP BY region;
  46. select region, COUNT(name) from cia GROUP BY region;
  47.  
  48.  
  49. /*Oppgave 2*/
  50.  
  51. select region, SUM(population) from cia GROUP BY region;
  52. select region from cia where region LIKE "%e%" group by region;
  53. select SUM(population) from cia where region LIKE "n%";
  54. select SUM(area) from cia where population>10000000;
  55. select name from cia where population>1000000 or area>10000;
  56. select name from cia where CHAR_LENGTH(name) = 7;
  57. select name from cia where population>area;
  58.  
  59.  
  60. /*Oppgave 3*/
  61.  
  62. /*
  63. <!DOCTYPE html>
  64. <html>
  65. <head>
  66. <meta charset="utf-8">
  67. <title>xdd</title>
  68. </head>
  69. <body>
  70.  
  71. <h1>Søk</h1>
  72.  
  73. <?php
  74. $server = "localhost";
  75. $username = "root";
  76. $password = "";
  77. $dbname = "cia";
  78.  
  79. // Create connection
  80. $conn = mysqli_connect($server, $username, $password, $dbname);
  81.  
  82. // Check connection
  83. if (!$conn) {
  84. die("Connection failed: " . mysqli_connect_error());
  85. }
  86. ?>
  87.  
  88. <p>Denne siden sender SQL kommandoen: "select name from cia where name LIKE "b%";"</p>
  89.  
  90. <?php
  91. if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  92. $sql = 'select name from cia where name LIKE "b%"';
  93. $results = mysqli_query($conn, $sql);
  94. if (!$results) {
  95. printf("Error: %s\n", mysqli_error($conn));
  96. exit();
  97. }
  98. while($row = mysqli_fetch_array($results)){
  99. echo $row["name"] . "<br>";
  100. }
  101. }
  102. ?>
  103.  
  104. </body>
  105. </html>
  106.  
  107. */
  108.  
  109.  
  110. /*Oppgave 4*/
  111. /*
  112. <!DOCTYPE html>
  113. <html>
  114. <head>
  115. <meta charset="utf-8">
  116. <title>xdd</title>
  117. </head>
  118. <body>
  119.  
  120. <h1>Søk</h1>
  121.  
  122. <?php
  123. $server = "localhost";
  124. $username = "root";
  125. $password = "";
  126. $dbname = "cia";
  127.  
  128. // Create connection
  129. $conn = mysqli_connect($server, $username, $password, $dbname);
  130.  
  131. // Check connection
  132. if (!$conn) {
  133. die("Connection failed: " . mysqli_connect_error());
  134. }
  135. ?>
  136.  
  137. <form method="POST">
  138. Søk:<br>
  139. <input type="text" name="search"><br>
  140. </form>
  141.  
  142. <table>
  143. <tr>
  144. <th>Name</th>
  145. <th>Region</th>
  146. <th>Area</th>
  147. <th>Population</th>
  148. <th>GDP</th>
  149. </tr>
  150. <?php
  151. if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  152. $sql = 'SELECT * FROM cia where name = "'.$_POST["search"].'"';
  153. $results = mysqli_query($conn, $sql);
  154. if (!$results) {
  155. printf("Error: %s\n", mysqli_error($conn));
  156. exit();
  157. }
  158. while($row = mysqli_fetch_array($results))
  159. {
  160.  
  161. echo "<tr>";
  162. echo "<td>" . $row['name'] . "</th>";
  163. echo "<td>" . $row['region'] . "</td>";
  164. echo "<td>" . $row['area'] . "</td>";
  165. echo "<td>" . $row['population'] . "</td>";
  166. echo "<td>" . $row['gdp'] . "</td>";
  167. echo "<tr>";
  168. }
  169. }
  170. ?>
  171. </table>
  172.  
  173. </body>
  174. </html>
  175. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement