Database operations
CREATE DATABASE Specifies the name of the DATABASE
Creating a databaseUSE database name
Select databaseshow tables
Display all tablesDrop Database Specifies the database name
Deleting a Database
Table operation
CREATE TABLE name (column type, column type)
Create a tableShow create table Specifies the name of the table
Displays table creation statements and detailsDesc table name
Shows the structure of the tableDROP TABLE TABLE name
Delete tableTruncate table table name
Clear the tableAlter table table name add field type
Add a table fieldAlter table name add type after table name
Adds a table field and inserts it after the specified field locationAlter table name add type first
Add the table field and insert the frontAlter table Table name modify New type of the field
Changing the field TypeAlter table name alter table name alter table name alter table name alter table name
Change the field name and change the typeAlter table table_name drop column;
Drop table fieldAlter table Old table name rename New table name
Modify the name of the table
Data manipulation
- Conditions of the query
where
SELECT * from t_doctor WHERE doctor_name = doctor_name;Copy the code
-
Order by ascending (ASC), descending (high to low)desc
-
GROUP BY GROUP
Grouping may not be easy to understand, but LET me give you a separate example:
If there is a nurse table, and the task list, the task of the nurse little sister is to follow up their responsible patients, a nurse little sister is responsible for many patients, all the nurses in the task list little sister, all the tasks performed. Now, let’s see how many missions each nurse has done
<!Select nurse_id from nurse_id
SELECT nurse_id,count(*) as tatal FROM t_visit_task GROUP BY nurse_id ORDER BY tatal desc;
Copy the code
with ROLLUP
Indicates whether the results of classification aggregation are resummarized
<! -- In addition to knowing how many missions each nurse performed, SELECT nurse_id,count(*) as tatal FROM t_visit_task GROUP BY nurse_id WITH ROLLUP;Copy the code
- Fuzzy query
like
<!SQL > select * from doctor_name;
SELECT * from t_doctor WHERE doctor_name LIKE "%张%";
Copy the code
in
The query
SELECT * FROM t_visit_task WHERE nurse_id in ("1126cf7f-d31e-43f1-a2dd-05b37873f99b","8ce40555-4b62-498b-8e39-af4abb18abfd"); <! SELECT * FROM t_visit_task where nurse_id = "1126CF7f-d31E-43F1-a2DD-05b37873f99b" OR nurse_id = "1126CF7f-d31E-43F1-a2DD-05b37873f99b "8ce40555-4b62-498b-8e39-af4abb18abfd"Copy the code
-
Insert into values(1, 2),(1, 2) insert into values(1, 2
-
Select * from table_name where table_name =’ XXX ‘and table_name =’ XXX’ where table_name =’ XXX ‘and table_name =’ XXX’ and table_name =’ XXX
-
Delete from table_name where table_name =”xx”; Delete data, must add conditions, or delete all
-
Union and union all combine the data results from multiple select queries (removing duplicate parts), and union all does not repeat
-
A Union must consist of two or more SELECT statements with Union links between them
-
Each query in a Union must contain the same columns, expressions, or aggregate functions, and they may appear in a different order.
-
Column data types must be compatible, which means that the database can implicitly convert their types
-
<!-- The first query 2020-10-13 after the entry of the nurse sister -->
<!-- The second query name contains Li's little sister, regardless of when she started -->
<!After execution, two SQL results may contain each other's existing data, union is repeated to merge display -->
SELECT * FROM t_nurse WHERE created_at > "2020-10-13"
UNION
SELECT * FROM t_nurse WHERE nurse_name LIKE "% %" li;
Copy the code
- Multi-table LianZha
inner join
Example Query the data that meets the association conditions on multiple tables
SELECT * FROM t_nurse INNER JOIN t_visit_task ON t_nurse.nurse_id = t_visit_task.nurse_id; <! SELECT * FROM t_nurse,t_visit_task WHERE t_nurse. Nurse_id = t_visit_task.nurse_id;Copy the code
left join
The left table is the main table, and the other tables are matched with the data given the associated conditions. If there is one, it will be displayed, and if there is no one, it will be empty
SELECT * FROM t_nurse LEFT JOIN t_visit_task ON t_nurse.nurse_id = t_visit_task.nurse_id; Copy the code
right join
The right table as the main table, other tables with the associated conditions of the data, there is a display, no empty
SELECT * FROM t_nurse RIGHT JOIN t_visit_task ON t_nurse.nurse_id = t_visit_task.nurse_id; Copy the code