MySql single table query exercise

 

Students watch students

Table-building statement:

 CREATE TABLE `students`  (

  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',

  `age` int (11) NULL DEFAULT NULL COMMENT 'age',

  `gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'M=男, F=女'

) ENGINE = InnoDB CHARACTER SET = utf8;

-Student table data:

INSERT INTO `students` VALUES ('张 一', 12, 'Male');

INSERT INTO `students` VALUES ('Zhang Er', 18, 'Male');

INSERT INTO `students` VALUES ('Zhang Er', 19, 'Male');

INSERT INTO `students` VALUES ('Zhang San', 122, 'Male');

INSERT INTO `students` VALUES ('Zhang Si', 123, 'Male');

INSERT INTO `students` VALUES ('Zhang Wu', 161, 'Male');

INSERT INTO `students` VALUES ('Zhang Liu', 66, 'Male');

INSERT INTO `students` VALUES ('张 七', 35, 'Female');

INSERT INTO `students` VALUES ('Zhang Ba', 55, 'Male');

INSERT INTO `students` VALUES ('张 九', 56, 'Male');

INSERT INTO `students` VALUES ('张 十', 96, 'Male');

INSERT INTO `students` VALUES ('Zhang Eleven', 11, 'Male'); 

 

Single table basic query

  1. 1) Query all data in the table
    SELECT * FROM students

    2) Display the NAME and age columns in the students table

    SELECT `name`,age FROM students;

    3) The same name is displayed only once

    SELECT * FROM students GROUP BY `name`

    4) The data in the query table is younger than 16 years old

    SELECT * FROM students WHERE age < 16

    5) Query data older than 18 years old, and arrange them in reverse order

    SELECT * FROM students WHERE  age > 18 ORDER BY age DESC

    6) Query the data between the ages of 18-30 (including 18, 30)

    SELECT * FROM students  WHERE  age BETWEEN 18 AND 30  

    # Query data between the ages of 18-30 (excluding 18, 30)

    SELECT * FROM students  WHERE  age >18  AND age <30

    7) Query all data that is not 66 years old

    SELECT * FROM  students WHERE age !=66

    8) Query the data of all names including "one"

    SELECT * FROM students WHERE `name` LIKE  '%一%' 

    9)统计表中男, 女的数量分别是多少

    SELECT gender 性别,COUNT(*) 数量 FROM students  GROUP BY gender   

    10)查询表中所有数据平均年龄

    SELECT  AVG(age) 平均年龄 FROM  students  

    11)查询最大年龄
    SELECT MAX(age) FROM students  

    # 延伸 查询最大年龄的所有信息
    SELECT  *  FROM students   WHERE   age=(SELECT MAX(age) FROM  students)

    # 查询最小年龄 
    SELECT MIN(age) FROM students  

 

                                                  ToBeContinue

发布了248 篇原创文章 · 获赞 362 · 访问量 32万+

Guess you like

Origin blog.csdn.net/bbvjx1314/article/details/105482145