distinct
select distinct country from website;
limit
select * from website limit 2;
SQL通配符:% ,_ (代替一個字符), [charlist]
select * from website where name like '_oogle'
select * from website where name REGEXP '^[GFS]'
IN操作符(與where相關,用於嵌套查詢): 在where中規定多個值
select * from website where name in ('Google','菜鳥')
Having操作符(與group by相關)
嵌套查詢:https://www.cnblogs.com/shengulong/p/10217146.html
別名:
select cloumn_name as alias_name;
select name,CONCAT(url,',',country) as site_info from website;
JOIN...ON: inner join、left join、right join、full join
select * from A inner join B on A.xx = B.xx
UNION:合併多個select語句結果集
select country from A
union
select country from B
order by country;
union / union all 區別:對重複結果的處理
Select into:表數據複製到另一個表
create table xxx select * from yyy
約束:NOT NULL、UNIQUE、PRIMARY KEY、FOREIGN KEY、CHECK、DEFAULT
- create table (...,...,unique(column_name));
- CONSTRANT xx unique(column1,column2);
- drop constrant xx
- 添加約束:alter table xxx add unique(column_name) drop unique ... primary key ...
- foreign key (column_name) references A(xx)
- check(column >0)
- alter table A alter column B set default 'xxx'
索引:
簡單索引:create index index_name on table_name(column_name);
唯一索引:create unique index index_name on table_name(column_name);
撤銷索引:drop index table_name.index_name
修改:
修改表名:alter table table_name rename to xxx
增加列:alter table table_name alter column column_name datatype(類型)
刪除列:alter table table_name drop column column_name datatype
視圖:
創建視圖:create view xx as ...
更新視圖:create or replace view xx as...
撤銷視圖:drop view view_name
SQL Aggregate函數
AVG、COUNT、FIRST(第一個記錄值)、LAST(列中最後一個記錄)、MAX、MIN、GROUP BY、HAVING、UCASE(字段大寫)、LCASE(字段小寫)、MID(從文本字段中提取字段)、LEN、ROUND、FORMAT
典型場景:https://blog.rxliuli.com/p/73dac442/
- 行轉列:select xx max(case xx when ...... then ... else 0 end) as xxx ............ from table group by xx
- 列轉行: union all\