The most commonly used SELECT statement in SQL, which is used to select data in a table.
The knowledge points to remember are as follows:
- SELECT Statement Format:
- SELECT the column name to query from the table name WHERE restriction condition;
After the WHERE statement:
- Mathematical symbol Conditions: = > < >= <= between and
- Logical character: And/or/in/not in
- Wildcard characters: like + _,%
- Sort: ORDER by + ASC, DPSC
- SQL built-in functions and calculations
- Subqueries and connection queries
Basic format for SELECT statements
SELECT the column name to query from the table name WHERE restriction condition;
-- querying all content of a table (such as employee tables) Select * from employee; -- information about name and age in the lookup table Select from employee;
What's behind (restriction, logic, wildcard, sort) mathematical symbol conditions
=, >, <, >=, <=, between
Logical character
And, or, in, not in
--Filter the names and ages of people older than 25SELECTName,age fromEmployeeWHEREAge> -;--find the Name,age and phone for an employee named MarySELECTName,age,phone fromEmployeeWHEREName='Mary';--filter out age less than 25, or age greater thanSELECTName,age fromEmployeeWHEREAge< - ORAge> -; --filter out age greater than 25 and age less thanSELECTName,age fromEmployeeWHEREAge> - andAge< -; --This situation can be combined with between and--filter out age less than 25, or age greater thanSELECTName,age fromEmployeeWHEREAgebetween - and -; --inquire about people in DPT3 or Dpt4SELECTName,age,phone,in_dpt fromEmployeeWHEREIn_dptinch('DPT3','Dpt4');--find people who are not in DPT1 and DPT3SELECTName,age,phone,in_dpt fromEmployeeWHEREIn_dpt not inch('dpt1','DPT3');
Wildcard characters
The keyword like is used in SQL statements and wildcards, which represent unknown characters. The wildcard characters in SQL are _ and%.
where _ represents an unspecified character , and% indicates an unspecified character .
-- The first four digits of the phone number are 1101, and then two are forgotten, you can use two _ wildcard characters instead: SELECT from WHERE like ' 1101__ ' ; -- another situation, such as only the first letter of the name, and do not know the length of the name, the% wildcard character instead of the indefinite characters SELECT from WHERE like ' j% ';
Sort the results
By default, the result of order by is arranged in ascending order, while the keyword ASC and desc can be used to specify ascending or descending sorting.
-- we sort by salary in descending order, and the SQL statement is:
SELECT from ORDER by DESC;
MySQL command: Select query statement