|
Techniques of Making SELECTions SELECTing One Column As opposed to selecting all columns from a table, you may be interested in only one particular column. To select a column, you can replace the column(s) placeholder of our formula with the name of the desired column. Here is an example: <!DOCTYPE html>
<html>
<head>
<title>Students Grades - Scales</title>
</head>
<body>
<h2>Students Grades - Scales</h2>
<?php
$link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise");
mysqli_connect_errno();
$students = $link->query("SELECT StudentNumber
FROM StaffMembers;");
while($staff = $students->fetch_array(MYSQLI_ASSOC))
{
echo "Student #: " . $staff["StudentNumber"] . "<br>";
}
$students->close();
$link->close();
?>
</body>
</html>
You can also qualify a column by preceding it with the name of the table followed by the period operator. Here is an example: SELECT StaffMembers.StudentNumber
FROM StaffMembers;
If you specify a column to select from a table, every record would come up. This can cause the same value to repeat over and over. Here is an example: <!DOCTYPE html>
<html>
<head>
<title>Students Grades - Scales</title>
</head>
<body>
<h2>Students Grades - Scales</h2>
<?php
$link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise");
mysqli_connect_errno();
$grades = $link->query("SELECT Descriptor
FROM GradesScales28");
while($grade = $grades->fetch_array(MYSQLI_ASSOC))
{
echo "Grade Descriptor: " . $grade["Descriptor"] . "<br>";
}
$grades->close();
$link->close();
?>
</body>
</html>
This would produce:
Notice that PG-13 and R are repeated. Sometimes you want to show each value only once. To get a list of distinct values, put the DISTINCT keyword between SELECT and the name of the column. Here is an example: <!DOCTYPE html>
<html>
<head>
<title>Students Grades - Scales</title>
</head>
<body>
<h2>Students Grades - Scales</h2>
<?php
$link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise");
mysqli_connect_errno();
$grades = $link->query("SELECT DISTINCT Descriptor
FROM GradesScales28");
while($grade = $grades->fetch_array(MYSQLI_ASSOC))
{
echo "Grade Descriptor: " . $grade["Descriptor"] . "<br>";
}
$grades->close();
$link->close();
?>
</body>
</html>
In most cases, you would get a better result if you select only one column. Still, you can use as many columns as you want.
SELECTing Many Fields of the Same Table To consider more than one column in a statement, you can list them in the column(s) placeholder of our formula, separating them with commas. The syntax you would use is: SELECT column1, column2, column_n FROM table-name; Here an example: <!DOCTYPE html>
<html>
<head>
<title>Students Grades - Scales</title>
</head>
<body>
<h2>Students Grades - Scales</h2>
<?php
$link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise");
mysqli_connect_errno();
$grades = $link->query("SELECT LetterGrade,
MinRange,
MaxRange,
MinPercent,
MaxPercent,
Descriptor
FROM GradesScales28");
echo "<table border=3><tr>";
echo " <td> </td><td colspan='2' align='center'><b>Range</b></td>";
echo " <td colspan='2' align='center'><b>Grade Percentage</b></td>";
echo " <td> </td>";
echo " </tr><tr><td><b>Letter Grade</b></td>";
echo " <td><b>Minimum</b></td><td><b>Maximum</b></td>";
echo " <td><b>Minimum</b></td><td><b>Maximum</b></td>";
echo " <td><b>Descriptor</b></td></tr>";
while($grade = $grades->fetch_array(MYSQLI_ASSOC))
{
echo "<tr><td>" . $grade["LetterGrade"] . "</td>";
echo "<td>" . $grade["MinRange"] . "</td>";
echo "<td>" . $grade["MaxRange"] . "</td>";
echo "<td>" . $grade["MinPercent"] . "</td>";
echo "<td>" . $grade["MaxPercent"] . "</td>";
echo "<td>" . $grade["Descriptor"] . "</td></tr>";
}
echo "</table>";
$grades->close();
$link->close();
?>
</body>
</html>
You can qualify each column by preceding it with the name of the table followed by the period operator. Here is an example: SELECT GradesScales.LetterGrade, GradesScales.MinRange, GradesScales.MaxRange, GradesScales.MinPercent, GradesScales.MaxPercent, GradesScales.Descriptor FROM GradesScales You don't have to qualify all columns, you can qualify some and not qualify some others. You can use an alias name for a table by preceding each column with the stated alias and a period. Here is an example: SELECT scales.LetterGrade, scales.MinRange, scales.MaxRange, scales.MinPercent, scales.MaxPercent, scales.Descriptor FROM GradesScales28 scales;
SELECTing Columns From Different Tables You can create a SELECT statement that selects columns from more than one table of a database. Neither the tables nor the columns need to have anything in common. The formula to follow is: SELECT column(s) FROM table_1, table_2, table_n You start with SELECT followed by the list of columns from the tables. If the tables have columns with different names, you can simply list the name of each column. Here is an example: <!DOCTYPE html>
<html>
<head>
<title>Students Grades - Scales</title>
</head>
<body>
<h2>Students Grades - Scales</h2>
<?php
$link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise");
mysqli_connect_errno();
$grades = $link->query("SELECT LetterGrade,
StudentNumber,
MinRange,
MaxRange,
MinPercent,
MaxPercent,
LastName,
Descriptor
FROM GradesScales28, StaffMembers;");
echo "<table border=3><tr>";
echo " <td> </td><td colspan='2' align='center'><b>Range</b></td>";
echo " <td colspan='2' align='center'><b>Grade Percentage</b></td>";
echo " <td colspan='3'> </td>";
echo " </tr><tr><td><b>Letter Grade</b></td>";
echo " <td><b>Minimum</b></td><td><b>Maximum</b></td>";
echo " <td><b>Minimum</b></td><td><b>Maximum</b></td>";
echo " <td><b>Descriptor</b></td>";
echo " <td><b>Student #</b></td>";
echo " <td><b>Last Name</b></td></tr>";
while($grade = $grades->fetch_array(MYSQLI_ASSOC))
{
echo "<tr><td>" . $grade["LetterGrade"] . "</td>";
echo "<td>" . $grade["MinRange"] . "</td>";
echo "<td>" . $grade["MaxRange"] . "</td>";
echo "<td>" . $grade["MinPercent"] . "</td>";
echo "<td>" . $grade["MaxPercent"] . "</td>";
echo "<td>" . $grade["Descriptor"] . "</td>";
echo "<td>" . $grade["StudentNumber"] . "</td>";
echo "<td>" . $grade["LastName"] . "</td></tr>";
}
echo "</table>";
$grades->close();
$link->close();
?>
</body>
</html>
When you select fields from different tables, in the result, each of the records of the first table would display, each showing the first record (combination of the selected columns) of the second table. Then each of the records of the first table would show again, followed by the second record (combination of the selected columns) of the second table. This will continue until all records of the second table have displayed. Consequently, the resulting query would contain (Number of Records of First Table) x (Number of Records of Second Table). For example, if the first table contains 4 records and the second table contains 2 records, the statement would produce 4 x 2 = 8 records. The above statement would produce:
|
|
|||||||
|
|