SELECT
The SELECT statement is used to select data from the database.
SELECT Columna_name,columnb_name from table_name;
Select the data from the table that selects columns A and B columns.
SELECT * from table_name;
Selects all data for the table.
First, DISTINCT keyword filtering repetition
SELECT DISTINCT Filter Duplicate results.
SELECT DISTINCT Column_name,column_name from table_name;
Do not repeat the selection, plus distinct will filter out the same results.
second, where clause
The WHERE clause is used to extract records that meet the specified criteria, and it selects Records.
SELECT Column_name,column_name from table_name WHERE column_name operator value;
Common operators in the WHERE clause:
operator |
Description |
= |
Equals |
<> |
Not equal to. Note: in some versions of SQL, this operator can be written in! = |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
Between |
Within a range |
Like |
Search for a pattern |
Inch |
Specify multiple possible values for a column |
operator when comparing, if the number field does not need to add ', if the non-numeric field needs to be added '.
The classification by operator is as follows:
1. Comparison operators
comparison operator Inclusions: = > < >=, <=,!=,<> (not equal)
SELECT * fromEmpWHEREEname='SMITH';SELECT * fromEmpWHEREename!='SMITH';SELECT * fromEmpWHEREEage= -;SELECT * fromEmpWHEREEage> -;
2. Logical operation
The logical operators are: and (and) or(or ) not (non).
SELECT * fromEmpWHERESal> - andSal< the;SELECT * fromEmpWHERESal> - OREname= 'SMITH';SELECT * fromEmpWHERE notSal> -;
Logical operator Precedence:()> not> and>OR
3. Special conditions
SELECT * from WHERE is NULL;
Columns that return null values
- Between ... and (returns the value within the interval)
SELECT * from WHERE between the and ;
Returns the value of the interval, which can be a number, can be an English character, or it can be a date. For example:
SELECT * fromWebsitesWHEREName not between 'A' and 'H';SELECT * fromAccess_logWHEREDatebetween '2016-05-10' and '2016-05-14';
Not between ... and (returns a value that is not within the interval)
SELECT * from WHERE inch (In the",");
Returns a column with a value equal to () , in can specify multiple values,= Specifies a value
SELECT * from WHERE like ' M% ';
% represents more than one word value, and an underscore denotes one character :
M% : For the wildcard, the regular expression, meaning that the fuzzy query information for the beginning of M.
%M% : Indicates that the query contains all the contents of M.
%m_ : Represents all the contents of the query with M in the penultimate position.
ThreeORDER by
The order by keyword is used to sort the result set by one column or multiple columns.
ASC Ascending,desc Descending. The default is descending. ASC Ascending, growing larger.
SELECT Column_name,column_name from table_name ORDER by ASC | DESC;
Sort By column B sequentially, and then arrange a on the basis of column B.
Fourtop keyword, LIMIT, rownum clause
The SELECT TOP clause is used to limit the number of records to be returned.
Note: Not all database systems support the SELECT TOP clause.
SQL server/ms Access Syntax
SELECT TOP Number | percent column_name (s) from table_name;
In SQL Server, you can use a percentage display: floating-point numeric +percent
MySQL syntax
SELECT column_name (s) from number;
Oracle syntax
SELECT column_name (s) from table_name WHERE <= number;
SQL statement (i)