-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDistinctMinimumMaximumAverageSQL.php
65 lines (51 loc) · 2.37 KB
/
DistinctMinimumMaximumAverageSQL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
// DistinctMinimumMaximumAverageSQL
function Distinct($field, $conn, $table){
$SQL = "SELECT DISTINCT `". $field ."` FROM `$table` ORDER BY `$table`.`" . $field . "` ASC";
$result = mysqli_query($conn, $SQL);
return $result->num_rows;
}
function Minimum($field, $conn, $table){
$SQL = "SELECT MIN(`". $field ."`) FROM `$table`";
return mysqli_query($conn, $SQL);
}
function Maximum($field, $conn, $table){
$SQL = "SELECT MAX(`". $field ."`) FROM `$table`";
return mysqli_query($conn, $SQL);
}
function Average($field, $conn, $table){
$SQL = "SELECT AVG(`". $field ."`) FROM `$table`";
return mysqli_query($conn, $SQL);
}
// DB Credentials
$hostname = "127.0.0.1";
$username = "CHANGE-TO-DB-USERNAME";
$dbname = "CHANGE-TO-YOUR-DATABASE";
$password = "CHANGE-TO-YOUR-PASSWORD";
$port = 3306;
$connection = mysqli_connect($hostname, $username, $password, $dbname, $port)or die(mysql_error());
// Example Use:
// Totals / sort each pertinent param
$ethnicityTotal = Distinct('ethnicity', $connection, "mock_data");
$genderTotal = Distinct('gender', $connection, "mock_data");
$primaryLanguagesTotal = Distinct('primaryLanguages', $connection, "mock_data");
$secondaryLanguagesTotal = Distinct('secondaryLanguages', $connection, "mock_data");
$sexualPreferenceTotal = Distinct('sexualPreference', $connection, "mock_data");
$zipCodeTotal = Distinct('zipCode', $connection, "mock_data");
$stateTotal = Distinct('state', $connection, "mock_data");
$relationshipPreferenceTotal = Distinct('relationshipPreference', $connection, "mock_data");
$religionTotal = Distinct('religion', $connection, "mock_data");
$politicalTotal = Distinct('political', $connection, "mock_data");
//--- MIN & MAX each pertinent param
$youngest = Maximum('birthDate', $connection, "mock_data");
$oldest = Minimum('birthDate', $connection, "mock_data");
$shortest = Minimum('height', $connection, "mock_data");
$tallest = Maximum('height', $connection, "mock_data");
$averageHeight = Average('height', $connection, "mock_data");
$fewestKids = Minimum('numberOfKids', $connection, "mock_data");
$mostKids = Maximum('numberOfKids', $connection, "mock_data");
$averageKids = Average('numberOfKids', $connection, "mock_data");
$lowestIncome = Minimum('annualIncome', $connection, "mock_data");
$highestIncome = Maximum('annualIncome', $connection, "mock_data");
$averageIncome = Average('annualIncome', $connection, "mock_data");
?>