MySQL command: Select query statement

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.