mysql query data
One, insert data
1. When creating a data table, you need to specify the primary key, or you can set the primary key to auto-increment AOTO_INCREMENT
2. If a column is set to auto-growth, even if you manually insert null, the data after the auto-growth will be inserted
3. If a column is set to grow automatically, if the value of the automatic growth column is manually specified, it will not grow automatically for a long time, and the value inserted manually shall prevail.
4. The specific value of self-growth is added by 1 to the highest historical value in the database
5. Code:
CREATE TABLE student (id int PRIMARY KEY AOTO_INCREMENT , name VARCHAR(20);//主键
INSERT INTO student (id , name) VALUES (1,'超人');
INSERT INTO student (name) VALUES('Wonder Woman');//The self-incrementing sequence can be used without inserting specific values
INSERT INTO student (id,name) VALUES (null,'Batman');//The self-incrementing sequence will not be affected by the insertion value of NULL
INSERT INTO student (id,name) VALUES (20,'Flash');//The auto-increment sequence is subject to the specified value
Two, simple query data sql statement
1. Conditional query keyword WHRER
2. Conditional judgment symbols, equal to (=), not equal to (!=), greater than (>), less than (<)
3. Judging between two values (including two boundary values): BETWEEN.....AND......
4. Determine several values: IN (data 1, data 2, ........)
5. Multiple conditions to judge AND (and), OR (or), NOT (not)
6. Judging the empty value: IS NULL (empty) IS NOT NULL (non-empty)
7. Code
sid | take off | age | gender |
S_1001 | spiderman | 30 | male |
S_1002 | superman | null | male |
S_1003 | wonder woman | 246 | male |
S_1004 | iron Man | 40 | male |
S_1005 | black widow | 36 | female |
S_1006 | Captain America | 235 | male |
S_1007 | Hulk | 45 | null |
S_1008 | Ant-Man | 30 | male |
S_1009 | flash | 22 | male |
SELECT sname,age FROM student WHERE age < 40 AND gender = 'female';
SELECT sname,age FROM student WHERE age BETWEEN 50 AND 30;
SELECT * FROM student WHERE sname IN ('Ant-Man','The Flash','Superman') AND age < 30;
SELECT sname,IFNULL(age,180) AS age FROM student ORDER BY age;
3. Sorting of simple query statements: ORDER BY
1. Keywords: Ascending (ASC), Descending (DESC)
2. Code:
SELECT * FROM student ORDER BY age DESC;
Fourth, the aggregation function
1. Alias: Use the AS keyword after the query column to give an alias to the virtual table after the query.
2. Aggregate function: find the number of rows count()
3. Maximum value: MAX() , Minimum value: MIN() , Summation: SUM() , Average value: AVG(),
4.count() will ignore NULL and only find the number of rows with value. If it is *, it represents the number of rows of all data.
5. Code
SELECT count(*) AS member FROM student;
SELECT MAX(age) FROM student WHRER age > 50;
5. Fuzzy query
1. Keyword LIKE
2.通配符: 下划线 _ 表示一个任意字符,百分号 % 表示任意个任意的字符
3.代码
SELECT * FROM student LIKE '美国__';
SELECT * FROM student LIKE '%铁%';