05, DQL: query records in the table

* select * from 表名;

1. Syntax:
    select
        field list
    from
        table name list
    where
        condition list
    group by
        grouping field
    having
        grouping conditions after grouping
    order by
        sorting
    limit
        paging limit

2. Basic query
    1. Multi-field query
        select field name 1, field name 2... from table name;
        * Note:
            * If you query all fields, you can use * to replace the field list.
    2. Removal of duplication:
        * distinct
    3. Calculating columns
        * Generally, four arithmetic operations can be used to calculate the values ​​of some columns. (Generally only numerical calculations are performed)
        * ifnull (expression 1, expression 2): the calculation that null participates in, the calculation result is all null
            * expression 1: which field needs to be judged whether it is null
            * if the field is null The replacement value after.
    4. Alias:
        * as: as can also be omitted
3. Conditional query
    1. Where clause followed by condition
    2. Operator
        *>, <, <=, >=, =, <>
        * BETWEEN...AND  
        * IN (Collection) 
        * LIKE: fuzzy query
            * Placeholder:
                * _: a single arbitrary character
                * %: Multiple arbitrary characters
        * IS NULL  
        * and or &&
        * or or || 
        * not or!
        

 -- 查询年龄大于20岁

            SELECT * FROM student WHERE age > 20;
            
            SELECT * FROM student WHERE age >= 20;
            
            -- 查询年龄等于20岁
            SELECT * FROM student WHERE age = 20;
            
            -- 查询年龄不等于20岁
            SELECT * FROM student WHERE age != 20;
            SELECT * FROM student WHERE age <> 20;
            
            -- 查询年龄大于等于20 小于等于30
            
            SELECT * FROM student WHERE age >= 20 &&  age <=30;
            SELECT * FROM student WHERE age >= 20 AND  age <=30;
            SELECT * FROM student WHERE age BETWEEN 20 AND 30;
            
            -- 查询年龄22岁,18岁,25岁的信息
            SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
            SELECT * FROM student WHERE age IN (22,18,25);
            
            -- 查询英语成绩为null
            SELECT * FROM student WHERE english = NULL; -- 不对的。null值不能使用 = (!=) 判断
            
            SELECT * FROM student WHERE english IS NULL;
            
            -- 查询英语成绩不为null
            SELECT * FROM student WHERE english  IS NOT NULL;

-- 查询姓马的有哪些? like
			SELECT * FROM student WHERE NAME LIKE '马%';
			-- 查询姓名第二个字是化的人
			
			SELECT * FROM student WHERE NAME LIKE "_化%";
			
			-- 查询姓名是3个字的人
			SELECT * FROM student WHERE NAME LIKE '___';

-- 查询姓名中包含德的人
			SELECT * FROM student WHERE NAME LIKE '%德%';

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/109005418