![]() |
List Arrangement |
The lists of records we got above with the SELECT statement were presented in the same order they were created in the table. SQL allows you to arrange records in alphabetical order, in chronological order or in numeric incremental order. After SELECTing a series of columns, you may want to list the records following an alphabetical order from one specific field. To get an alphabetical or an incremental order of records, you must let the database know what field would be used as reference. In SQL, this is done using the ORDER BY expression. The syntax used would be: SELECT What FROM WhatObject ORDER BY WhatField; The column used as the basis must be recognized as part of the selected columns. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT LastName, FirstName, Gender " & _ "FROM Persons " & _ "ORDER BY LastName;", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub Rearranging a list of records is referred to as sorting:
By default, records are ordered in ascending order. Nevertheless, the ascending order is controlled using the ASC keyword specified after the based field. For example, to sort the last names in ascending order including the first and last names, execute the following statement: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT LastName, FirstName, Gender " & _ "FROM Persons " & _ "ORDER BY LastName ASC;", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
If you want to sort records in reverse order, you can use the DESC keywords instead. It produces the opposite result to the ASC effect. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT LastName, FirstName, Gender " & _ "FROM Persons " & _ "ORDER BY LastName DESC;", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
Instead of using all data as we have done so far using the SELECT keyword, you can present a condition that the database would follow to isolate specific records. One of the keywords you can use to formulate conditions is WHERE. Its basic syntax is: SELECT What FROM WhatObject WHERE Expression; The expressions used in conditions are built using algebraic, logical, and string operators. The Expression factor is called a criterion. Although a group of expressions, making it plural is called criteria, the word criteria is sometimes used for a singular expression also. The expression is written using the formula: ColumnName=Value The ColumnName factor must be an existing column of the table. It is followed by the assignment operator. The Value factor is the value that would set the condition. If the value is a word or a group of words (also called a string), you must include it in single-quotes. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE Gender='Male';", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub If it is a number, you can type its numeric value.
In Boolean algebra, something is considered TRUE when it holds a value. The value is also considered as 1 or Yes. By contrast, if something doesn't hold a value, it is considered non-existent and non-worthy of consideration. Such a thing has a value of FALSE, 0, or No. To retrieve such a value, you can just find out if the value of a field exists or not. The comparison for a True or False value can be performed on a Boolean column. If a record has a value of 1, the table considers that such a field is True. If the field has a 0 value, then it holds a FALSE value.
While the True and False constants are used to find out whether a Boolean expression has a positive or a negative value, the database provides another constant used to find out whether a field is empty. This can be done using the NULL constant. When a field holds a value, the value would be considered using the comparison operators we will learn shortly. If the field is empty, it holds a value of NULL.
The equality operator is used to compare two values for similarity. The syntax of this operation is: Value1 = Value2 If Value1 and Value2 hold the same value, the comparison produces a TRUE result. If they hold different values, the comparison renders a FALSE value. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE Gender='Male';", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
To find out if two fields hold different values, you can use the inequality operator which is represented by <>. Its syntax is: Value1 <> Value2 This comparison is performed between Value1 and Value2. If they hold different values, then the comparison produces a TRUE value. If they hold the same value, the comparison produces FALSE. This shows that the equality (=) and the inequality (<>) operators are opposite each other. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE Gender<>'Female';", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
The "Less Than" operator uses the following syntax: Value1 < Value2 If Value1 holds a value that is lower than that of Value2, the comparison produces TRUE. If Value1 holds a value that is greater than or similar to that of Value2, the comparison renders FALSE. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\Students.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Students " & _ "WHERE DOB < '1988-06-01';", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
When comparing two values, you may want to know whether two fields hold the same value or if one is lower than the other. This comparison can be performed with the "Less Than Or Equal To" operator. It is represented by <= and its formula is: Value1 <= Value2 If both operands (Value1 and Value2) hold the same value, then the comparison produces a TRUE result. If Value1 holds a value that is lower than that of Value2, the comparison still produces a TRUE result. By contrast, if the value of Value1 is higher than that of Value2, the comparison renders a FALSE result. Notice that the > and the <= operators are opposite each other. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\Students.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Students " & _ "WHERE DOB =< '1988-06-01';", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
The > operator is used to find out whether one value is "Greater Than" another. Its syntax is: Value1 > Value2 The operation is performed on the values of Value1 and Value2. If Value1 holds a value greater than that of Value2, then the comparison produces TRUE. Otherwise, the comparison produces FALSE. That is, if the value of Value2 is greater than or equal to that of Value1, then the comparison produces FALSE.
If you have two values and want to find out whether they hold similar values or the first is greater than the second, you can use the >= operator whose syntax is: Value1 >= Value2 If both Value1 and Value2 hold the same value, then the comparison renders a TRUE result. Similarly, if the left operand, Value1, holds a value greater than that of the right operand, Value2, the comparison still produces TRUE. If the value of Value1 is less than the value of Value2, the comparison produces a FALSE result. Therefore, < and >= are opposite.
To deny the presence, the availability, or the existence of a value, you can use the NOT operator. This operator is primarily used to reverse a Boolean value. For example, we have learned that FALSE is the opposite of TRUE. In the same way, TRUE is the opposite of FALSE. If you want to compare a value as not being TRUE, the NOT TRUE would produce the same result as the FALSE value. For the same reason, the expression NOT FALSE is the same as TRUE. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE NOT (Gender = 'Male');", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
To validate something as being possible, you can use the IS operator. For example, to acknowledge that something is NULL, you can use the IS NULL expression. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE Gender IS NULL;", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub In the same way, to validate that something is not null, you can use the expression IS NOT NULL. Here is an example: Private Sub btnRecordset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRecordset.Click Dim rstPeople As ADODB.Recordset = New ADODB.Recordset Dim conADO As ADODB.Connection = New ADODB.Connection conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\People.mdb'", Nothing, Nothing, 0) rstPeople.Open("SELECT * FROM Persons " & _ "WHERE Gender IS NOT NULL;", _ conADO, _ ADODB.CursorTypeEnum.adOpenDynamic, _ ADODB.LockTypeEnum.adLockOptimistic, 0) rstPeople.Close() End Sub
If you have a series of records and want to find a record or a group of records among them, you can use the IN operator. |
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|