Precautions:
sql uses single quotes to surround text values (most database systems also accept double quotes). If it's a number, don't use quotes.
1. Database
1. Create a database
CREATE DATABASE lesson ;
Create a database named lesson
2. Delete the database
drop database lesson ;
Two: table
1. Create a table
CREATE TABLE `mydb`.`it` (
`id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`)
);
Create a table in the database named mydb, the name of the table is it.
There are two fields in the table: id , name . where the primary key is id
2, delete the table
DROP TABLE `mydb`.`it`;
Database name: mydb
table name: it
3. Modify the name of the table
RENAME TABLE student TO user ;
Change the name of the student table to user
4. Copy table
create table table2 select * from user ;
Create a new table table2 by copying the user table
Three: add data
1. Insert data into the table
INSERT INTO `mydb`.`user` (`id`, `name`, `age`) VALUES ('2', 'lisid', '25');
mydb : database name
user : table name
id , name , age : represent three fields
VALUES : data corresponding to the field
Four: delete data
1. Conditional delete (delete a row of data)
DELETE FROM user WHERE id=1 ;
delete all data with id = 1 (delete the row with id = 1)
5. Update data
1. Update the value of a field
UPDATE `mydb`.`user` SET `age`='22' WHERE `id`='1';
data name: mydb
table name: user
field: age
filter condition: id = 1 ;
In the mydb database, in the user table, change the value of the age attribute of the row with id = 1 to 22
2. Update the values of multiple fields
UPDATE `mydb`.`user` SET `name`='奶茶妹妹', `age`='18' WHERE `id`='2';
In the mydb database, in the user table, change the name attribute value of the row with id = 2 to Sister Milk Tea, and the age attribute value to 22
Six: query data
1. Query
SELECT * FROM mydb.user;
Query all data in the user table from the mydb database
2. Single attribute query
SELECT name FROM user ;
Query all the name attribute values from the user table
3. Multi-attribute query
SELECT name , age FROM user ;
Query all the attribute values of name and age from the user table
4. Conditional query where
1. Query by id
SELECT name , age , id FROM user where id = 2 ;
Query all the attribute values of name, age, and id whose id is equal to 2 from the user table. Note that the type of id is an integer, so 2 does not need to add single quotes
2. Query by name
SELECT name , age , id FROM user where name = 'tom' ;
Query all attribute values of name, age, and id whose name value is equal to 'tom' from the user table. Note that the type of name is character type, and 'tom' is added with single quotes
3. Logical query: greater than
SELECT * FROM `user` WHERE id > 5 ;
Query all attribute values of name, age, and id with an id value greater than 5 from the user table.
All logical operators are supported: greater than> , less than < , greater than or equal to >= , less than or equal to <= , equal to = , not equal!= (or <>)
4. Logical query: BETWEEN
SELECT * FROM `user` WHERE id BETWEEN 3 AND 8 ;
user : table name
id : field name
between : The function of the operator is to select the data range between two values
Query all attribute values of name, age, and id with id values between 3 (including 3) and (including 8) from the user table. That is, the id is greater than or equal to 3 and less than or equal to 8.
5. Logical query: in
SELECT * FROM `user` WHERE age in ( 12 , 18 ) ;
user : table name
age : field name
in : The role of the operator is to specify multiple values for the query condition
Query all information about people aged 12 and 18 from the user table. Equivalent to union.
5. Fuzzy query like
1. Fuzzy query: start with a character
SELECT * FROM student WHERE phone LIKE '1825%'
student : table name
phone : field name represents mobile phone number
LIKE : fuzzy query
% : Represents any one or more characters, which can match characters of any type and length.
Find out the mobile number starting with 1825 from the student table
2. Fuzzy query: multiple conditions
SELECT * FROM student WHERE phone LIKE '182%' AND `name` LIKE 'zhang%' ;
student : table name
phone : The field name represents the phone number
name : The field name represents the name
. Find out all the information of the mobile phone number starting with 182 and the name starting with zhang from the student table
3. Fuzzy query: wildcard
SELECT * FROM student WHERE phone LIKE '1825110114_'
student : table name
phone : The field name represents the phone number
LIKE : fuzzy query
_ : represents a character
Find out the mobile number starting with 1825110114 from the student table
5. Fuzzy query: contains
SELECT * FROM student WHERE phone LIKE '%114%' ;
student : table name
phone : The field name represents the phone number
LIKE : fuzzy query
% : Represents any one or more characters, which can match characters of any type and length.
Find out all the information of the mobile phone number including 114 from the student table
6. Fuzzy query: cancel wildcards
SELECT * FROM student WHERE phone LIKE '%114\%' ;
student : table name
phone : The field name represents the phone number
LIKE : fuzzy query
% : Represents any one or more characters, which can match characters of any type and length.
Find out the mobile phone number ending with 114% from the student table, the first % stands for wildcard, % stands for % character, there is no wildcard function
6. Quantity limit query limit
select * from user order by id limit 3 , 5 ;
The query from the user table starts from 3 (including 3), and the number is 5 pieces of data. That is, 3, 4, 5, 6, 7 pieces of data
Note that 5 represents the number of queries
7. Single field sorting query order by
The order by statement is used to sort the result set according to the specified column, by default in ascending order.
select * from mydb.`user` order by age ;
mydb : database name
user : table name
age : Sort field
Query all the information from the user table, and the query results are sorted by the age field. (default ascending order)
select * from mydb.`user` order by age DESC ;
Query all the information from the user table, and the query results are sorted in descending order by the age field.
8. Multi-field sorting query order by
select * from mydb.`user` order by age DESC , `name` ;
mydb : database
user : table name
age : age
name : first name
Query all the information from the user table, and the query results are sorted in descending order of age. If the ages are the same, they are sorted in ascending order by name.
9. Find the total count()
SELECT COUNT( name ) FROM `user` ;
name : field name, representing the name
user : table name
Query the number of name values from the user table, excluding null values.
SELECT COUNT( * ) FROM `user` ;
Query the number of records in the user table
SELECT COUNT( DISTINCT `name` ) FROM `user` ;
Query the number of different values of the name field in the user table, which is equivalent to deduplication.
10. Find the maximum value max() and the minimum value min()
MIN and MAX can also be used with text columns to get the highest or lowest value in alphabetical order.
SELECT MAX( id ) FROM `user` ;
Query the maximum value of the id field from the user table
SELECT MIN( id ) FROM `user` ;
Query the minimum value of the id field from the user table
11. Sum ()
SELECT SUM( id ) FROM `user` ;
id : field name
user : table name
Query the sum of the id values in the user table
12. Average avg()
SELECT AVG( id ) FROM `user` ;
id : field name
user : table name
Query the average number of id values in the user table
13. Display all databases
SHOW DATABASES;
14. Display all tables
SHOW TABLES;
Seven: View
1. The basic concept of view
http://www.cnblogs.com/zzwlovegfj/archive/2012/06/23/2559596.html
2. Create a view
CREATE VIEW myview as SELECT * FROM mydb.`user` ;
myview : the name of the view
mydb : the name of the database
user : the name of the table
Query all the data of the user table as the data of the view myview
3, delete the view
drop view myview ;
myview : the name of the view
Eight: Subqueries
1. Query the first 5 pieces of data whose status field is 0 in the video table, and sort them in descending order by id
select t.* from (select * from video where status = 0 limit 5) as t order by t.id desc ;
Here is select * from video where status = 0 limit 5
equivalent to a temporary table, replaced by an alias t
.
Personal WeChat ID: zhaoyanjun125 , welcome to follow