1. SELECT statement
In the SQL world, the most basic operation is the SELECT statement. When using SQL directly under the database tool, many people will be familiar with the following operations:
The code copy is as follows:
SELECTwhatFROMwhichTableWHEREcriteria
Executing the above statement will create a query that stores its results.
On ASP page files, you can also use the above general syntax, but the situation is slightly different. When programming ASP, the content of the ELECT statement should be assigned to a variable as a string:
The code copy is as follows:
SQL="SELECTwhatFROMwhichTableWHEREcriteria"
Okay, I understand the way SQL "talk" under ASP, and then do it the same. As long as your needs are met, the traditional SQL query mode and conditional query can be useful.
For example, you might as well assume that there is a data table in your database, the name is Products, and now you want to retrieve all the records in this table. Then you wrote the following code:
The code copy is as follows:
SQL="SELECT*FROMProducts"
The above code - the function of the SQL statement is to retrieve all the data in the table - after execution, all records in the data table will be selected. However, if you only want to remove a specific column from the table, such as p_name. Then you can't use the * wildcard character. You have to type the name of a specific column here, the code is as follows:
The code copy is as follows:
SQL="SELECTp_nameFROMProducts"
After executing the above query, all the contents in the Products table and the p_name column will be selected.
2. Set query conditions in WHERE clause
For example, if you only plan to take out the p_name records, and the names of these records must be started with the letter w, then you have to use the following WHERE clause:
The code copy is as follows:
SQL="SELECTp_nameFROMProductsWHEREp_nameLIKE'W%'"
The WHERE keyword is followed by the conditions used to filter data. With the help of these conditions, only data that meets certain standards will be queried. In the above example, the query result will only get a p_name record with the name starting with w.
In the above example, the percentage symbol (%) means that the query returns all record entries that start with w letters and are followed by any data or even no data. Therefore, when executing the above query, west and willow will be selected from the Products table and stored in the query.
As you can see, as long as you carefully design the SELECT statement, you can limit the amount of information returned in the recordset. Thinking more will always meet your requirements.
These are just the beginning of mastering SQL usage. To help you gradually master the usage of complex SELECT statements, let's take a look at the key standard terms: comparison operators. These things are often used when you build your own SELECT strings to obtain specific data.