Table of contents
Database addition, deletion, modification and query SQL statements
View the structure of the specified table
Commenting on database commands
The SQL library provides functions about time: now()
Query table for column-to-column operations
select column name, column name + column name + column name from table name;
The query specifies a column as an alias, using the keyword as
To deduplicate query data, use the keyword distinct
Range search difference: between A and B (closed before and closed after)
Query a specific value: use in to represent
Fuzzy search for a character: use like
Paging query, use limit to query
The query result is added to the table as data
String interception of SQL query results: substring
String concatenation of SQL query results: concat
Character capitalization in SQL query results: upper
Find the maximum and minimum values of a column: max \ min
Conditional filter: having \ where
Count the number of rows in the table: count
Left outer join: left join table on
Right outer join: right join table on
Combine multi-table query results: union \ union all
Database addition, deletion, modification and query SQL statements
userlogin is the name of the table
增:insert into userlogin values ('5','5');
删:delete from userlogin where username ='3';
改:update userlogin set username ='3' where pwd ='2';
查:select * from userlogi;
Note: Try not to use the wildcard * in the query statement. Reason: When the amount of data is large, the query efficiency will be reduced. Use the column name instead of *, and develop a good habit at ordinary times.
MySQL database command
1. Query the database
show databases; (plural for multiple databases)
2. Create a database
create database database name charset utf8;
(The database name is composed of letters, numbers, and underscores, and the numbers cannot be at the top)
Note: here is database, there is no s behind it, and adding charset utf8 to the database can make the database insert Chinese, otherwise it will report an error when inserting Chinese
Error case:
3. Delete the database
drop database database name;
4. Select the database
use database name;
create table
Points to note (select a specific database before creating a table)
view all tables
show tables;
create table
create table table name (column name type, column name type, column name type);
Note: the types are int, varchar(), decimal(m, n) m means there are m significant digits, n means there are n digits after the decimal point
View the structure of the specified table
desc table name;
delete table
drop table table name;
Commenting on database commands
In SQL, you can use "--space + description" to indicate the comment description
CRUD detailed description
Increase
Insert into table name (id, name, age) values (1, 'Xiaohong', 111) Note: When inserting strings, remember to separate them with semicolons' '
Note: insert multiple at a time, remember to separate them with commas
The SQL library provides functions about time: now()
Inquire
Select * from table name;
Select column name, column name from table name;
Query table for column-to-column operations
select column name, column name + column name + column name from table name;
The query specifies a column as an alias, using the keyword as
To deduplicate query data, use the keyword distinct
Query result sorting
When the query results need to be sorted, you can use order by asc (asc can not be written by default), that is, order by
Perform ascending operations on the results (from small to large): order by
Perform descending operations on the results (from large to small): order by desc
Points to note: ( orde by can be followed by an alias , for example, when calculating the sum, there is as total in front, and order by total can be followed), Where cannot be followed by an alias
Specify condition query where
Conditional query, you can directly compare two columns
Example: Query the list of students whose Chinese scores are greater than their math scores
select name from table where chinese > math;
Range search difference: between A and B (closed before and closed after)
Query a specific value: use in to represent
Fuzzy search for a character: use like
Paging query, use limit to query
Example: Query the first 3 pieces of data in the table (the default is to query from the first page)
If you want to customize the query from a certain page, use offset
The query result is added to the table as data
String interception of SQL query results: substring
Usage: substring(string, start position of interception, number of characters of interception)
String concatenation of SQL query results: concat
Usage: concat(string1, string2, string3, ...)
Character capitalization in SQL query results: upper
Usage: upper(string)
aggregation query
Data summation: sum
Sum summation is only valid for numbers, and strings cannot be summed. When summing columns, rows with null results will be automatically skipped
Calculate the average: avg
Avg can calculate the average value of a column, the method of use is the same as sum
avg can also be used with expressions
Find the maximum and minimum values of a column: max \ min
Group query: group by
A group query will group the same columns in the same group.
For example, if you need to calculate the salary of a certain position, you need to divide each position into the same group, and then calculate the average salary of this position
Conditional filter: having \ where
where: filter the specified line
having: filter groups, used in conjunction with group by
Note: After the group by clause is grouped, when you need to conditionally filter the grouped results, you cannot use the where statement, but you need to use having
Count the number of rows in the table: count
select count(*) from table name;
Note: When it comes to multiple data calculations, use count and remember to use group by
Multi-table query: join on
When querying data in different columns from multiple tables, you need to use the multi-table query statement join on
Statement usage: select *from table 1 join table 2 on condition 1 join table 3 on condition 2
Example:
select * from student join score on student.id = score.id join course on course.id = score.course_id;
First consider the outer join between the left table and the middle table to get a "temporary" table, and then take this "temporary" table and the right table for outer join
Left outer join: left join table on
left join ---- Left join, take the columns of the left table as the main, take the intersection of the two columns, and take null for the columns that do not exist in the right column
Left outer join: table 1 left join table 2 on connection condition, the right table (table 2) returns the data that exactly matches the connection condition, and the left table (table 1) not only returns the data that matches the connection condition, but also the unmatched data will return;
Right outer join: right join table on
right join ---- Right join, take the columns of the right table as the main, take the intersection of the two columns, and take null for the columns that do not exist in the left column
Right outer join: table 1 right join table 2 on connection condition, the left table (table 1) returns the data that exactly matches the connection condition, and the right table (table 2) returns the data that matches the connection condition, and the data that does not match will return
Combine multi-table query results: union \ union all
union can combine the query results of multiple tables, the prerequisite is that multiple result columns must have one-to-one correspondence.
union : Perform a union operation on two query results, automatically remove duplicates, excluding duplicate rows , that is, remove duplicate results before displaying
union all : Perform a union operation on two query results without deduplication, including duplicate rows , that is, all query results will be displayed
database index
view index
show index from table name;
create index
create index index name on table name (column name);
delete index
drop index index name on table name;