建立數據庫:mysql
create database database-name
刪除數據庫:sql
drop database database-name
修改數據名:數據庫
RENAME DATABASE db_name TO new_db_name
建立新表:測試
create table table-name( col1-name type1 [not null] [primary key] [auto_increment], col2-name type2 , …… )
auto_increment ->自動增加,只針對數值類型ui
根據舊錶建立新表:spa
create table table_name like table_old_name
create table table_name as select col1,col2... from table_old_name;
刪除表:3d
drop table table_name;
修改表名:code
alter table table_name rename table_new_name;
增長一個列:sqlite
alter table table_name add column col type [not null] ...;
刪除一個列:blog
alter table table_name drop column col_name;
修改列名:
alter table table_name change column col_old_name col_new_name type [not null]...;
修改列的屬性:
alter table table_name change column col_old_name col_old_name type [not null]...;
選擇:
select * from table_name where ...
插入:
insert into table_name(col1_name,col2_name...) values(...);
拷貝表全部數據:
insert into table1_name(table1_name.col1_name,table1_name.col2_name...) select table2_name.col1_name, table2_name.col_name... from table2_name
跨數據庫之間表的拷貝(具體數據使用絕對路徑) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具體數據庫’ where 條件
刪除:
delect from table_name where
更新:
update table_name set col1_nameee=value ...where ...
查找:
select * from table_name where ...
排序:
select * from table_name order by col1_name asc/desc;
asc 升序
desc 降序
總數:
select count(*|col_name) as count_name from table_name where ...
求和:
select sum(col_name) as sum_name from table_name where ...
求均值:
select avg(col_name) as avg_name from table_name where ...
最大:
select max(col_name) as max_name from table_name where ...
最小:
select min(col_name) as min_name from table_name where ...
union和union all
UNION 用於合併兩個或多個 SELECT 語句的結果集,並消去表中任何重複行。
UNION 內部的 SELECT 語句必須擁有相同數量的列,列也必須擁有類似的數據類型。
同時,每條 SELECT 語句中的列的順序必須相同.
**註釋:另外,UNION 結果集中的列名老是等於 UNION 中第一個 SELECT 語句中的列名。
注意:一、UNION 結果集中的列名老是等於第一個 SELECT 語句中的列名
二、UNION 內部的 SELECT 語句必須擁有相同數量的列。列也必須擁有類似的數據類型。同時,每條 SELECT 語句中的列的順序必須相同
**
union的用法及注意事項
union:聯合的意思,即把兩次或屢次查詢結果合併起來。
要求:兩次查詢的列數必須一致
推薦:列的類型能夠不同,但推薦查詢的每一列,想對應的類型以同樣
能夠來自多張表的數據:屢次sql語句取出的列名能夠不一致,此時以第一個sql語句的列名爲準。
若是不一樣的語句中取出的行,有徹底相同(這裏表示的是每一個列的值都相同),那麼union會將相同的行合併,最終只保留一行。也能夠這樣理解,union會去掉重複的行。
若是不想去掉重複的行,可使用union all。
若是子句中有order by,limit,需用括號()包起來。推薦放到全部子句以後,即對最終合併的結果來排序或篩選。
SELECT column_name FROM table1 UNION SELECT column_name FROM table2
Join語法概述
JOIN 按照功能大體分爲以下三類:
INNER JOIN(內鏈接,或等值鏈接):取得兩個表中存在鏈接匹配關係的記錄。
select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where ...
select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where table1_name.col is null or table2_name.col is null and...
LEFT JOIN(左鏈接):取得左表(table1)徹底記錄,便是右表(table2)並沒有對應匹配記錄。
select * from table1_name left join table2_name on teable1_name.col = teable2_name.col where ...
select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where teable2_name.col is null and ...
RIGHT JOIN(右鏈接):與 LEFT JOIN 相反,取得右表(table2)徹底記錄,便是左表(table1)並沒有匹配對應記錄。
Full join:
select * from A left join B on B.name = A.name union select * from A right join B on B.name = A.name;
分組:
select *|count,avg,sum,max,min from table_name group by table_name.col_name
子查詢:
#in select * from table1_name where tabel1_name.col1 in ( select table2_name.col1 from teable2_name where... ) #not in select * from table1_name where tabel1_name.col1 not in ( select table2_name.col1 from teable2_name where... )
between 數值1 and 數值2
select * from table_name where table_name.col between 數值1 and 數值2
not between 數值1 and 數值2
select * from table_name where table_name.col between 數值1 and 數值2
兩張關聯表,刪除主表中已經在副表中沒有的信息:
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
limit 返回前幾條或者中間某幾行數據
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
having字句可讓咱們篩選成組後的各類數據,where字句在聚合前先篩選記錄,也就是說做用在group by和having字句前。而 having子句在聚合後對組記錄進行篩選。
例:
SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region HAVING SUM(area)>1000000
去重:
有時須要查詢出某個字段不重複的記錄,這時可使用mysql提供的distinct這個關鍵字來過濾重複的記錄。
select distinct col_name from table_name;
題目在線測試地址:https://www.nowcoder.com/ta/sql
無
emp_no | birth_date | first_name | last_name | gender | hire_date |
---|---|---|---|---|---|
10008 | 1958-02-19 | Saniya | Kalloufi | M | 1994-09-15 |
題解:
select * from employees order by hire_date desc limit 0,1;
無
emp_no | birth_date | first_name | last_name | gender | hire_date |
---|---|---|---|---|---|
10005 | 1955-01-21 | Kyoichi | Maliniak | M | 1989-09-12 |
select * from employees order by hire_date desc limit 2,1;
無
emp_no | salary | from_date | to_date | dept_no |
---|---|---|---|---|
10002 | 72527 | 2001-08-02 | 9999-01-01 | d001 |
10004 | 74057 | 2001-11-27 | 9999-01-01 | d004 |
10005 | 94692 | 2001-09-09 | 9999-01-01 | d003 |
10006 | 43311 | 2001-08-02 | 9999-01-01 | d002 |
10010 | 94409 | 2001-11-23 | 9999-01-01 | d006 |
select salaries.*,dept_manager.dept_no from salaries, dept_manager where salaries.emp_no = dept_manager.emp_no and salaries.to_date = "9999-01-01" and dept_manager.to_date = "9999-01-01";
無
last_name | first_name | dept_no |
---|---|---|
Facello | Georgi | d001 |
省略 | 省略 | 省略 |
Piveteau | Duangkaew | d006 |
select last_name,first_name,dept_emp.dept_no from employees,dept_emp where employees.emp_no=dept_emp.emp_no;
無
last_name | first_name | dept_no |
---|---|---|
Facello | Georgi | d001 |
省略 | 省略 | 省略 |
Sluis | Mary | NULL(在sqlite中此處爲空,MySQL爲NULL) |
select employees.last_name,employees.first_name,dept_emp.dept_no from employees left join dept_emp on employees.emp_no=dept_emp.emp_no;
無
emp_no | salary |
---|---|
10011 | 25828 |
省略 | 省略 |
10001 | 60117 |
select employees.emp_no,salaries.salary from employees inner join salaries on employees.emp_no = salaries.emp_no where employees.hire_date = salaries.from_date order by employees.emp_no desc;
無
emp_no | t |
---|---|
10001 | 17 |
10004 | 16 |
10009 | 18 |
select salaries.emp_no,count(salaries.salary) as t from salaries group by salaries.emp_no having count(salaries.salary)> 15;
無
salary |
---|
94692 |
94409 |
88958 |
88070 |
74057 |
72527 |
59755 |
43311 |
25828 |
select salaries.salary from salaries where to_date='9999-01-01' group by salary order by salary desc;
無
dept_no | emp_no | salary |
---|---|---|
d001 | 10002 | 72527 |
d004 | 10004 | 74057 |
d003 | 10005 | 94692 |
d002 | 10006 | 43311 |
d006 | 10010 | 94409 |
select dm.dept_no,dm.emp_no,s.salary from salaries s, dept_manager dm where s.emp_no=dm.emp_no and dm.to_date='9999-01-01' and s.to_date='9999-01-01'
無
emp_no |
---|
10001 |
10003 |
10007 |
10008 |
10009 |
10011 |
select emp_no from employees where employees.emp_no not in ( select emp_no from dept_manager );
無
emp_no | manager_no |
---|---|
10001 | 10002 |
10003 | 10004 |
10009 | 10010 |
select dept_emp.emp_no,dept_manager.emp_no as manager_no from dept_emp left join dept_manager on dept_emp.dept_no = dept_manager.dept_no where dept_emp.emp_no!=dept_manager.emp_no and dept_emp.to_date='9999-01-01' and dept_manager.to_date='9999-01-01';
無
dept_no | emp_no | salary |
---|---|---|
d001 | 10001 | 88958 |
d002 | 10006 | 43311 |
d003 | 10005 | 94692 |
d004 | 10004 | 74057 |
d005 | 10007 | 88070 |
d006 | 10009 | 95409 |
select dept_emp.dept_no,dept_emp.emp_no,max(salaries.salary) as salary from dept_emp,salaries where dept_emp.emp_no=salaries.emp_no and dept_emp.to_date='9999-01-01' and salaries.to_date='9999-01-01' group by dept_emp.dept_no;