標籤:
一、常用sql語句:
1、查詢:
select * from table where 1 = 1
2、增加:
insert into table(name,age) values("張三",25)
3、刪除:
delete from table where 1 = 1
4、修改:
update table set field1=value1 where 1 = 1
5、模糊查詢:like關鍵字
select * from table where name like ’%張*%’
6、排序:
select * from table order by name,age [desc]
7、計數:
select count(*) as totalcount from table
8、求和:
select sum("tableId") as sumvalue from table
9、平均值:
select avg("age") as avgvalue from table
10、最大:
select max("age") as maxvalue from table
11、最小:
select min("age") as minvalue from table二、其他sql
1、串連:join on left join right join
表A:
| a1 |
b1 |
c1 |
| 01 |
數學 |
90 |
| 02 |
語文 |
80 |
| 03 |
英語 |
70 |
表B:
select A.*,B.* from A inner join B on(A.a1=B.a2)
| a1 |
b1 |
c1 |
a2 |
b2 |
| 01 |
數學 |
90 |
01 |
張三 |
| 02 |
語文 |
80 |
02 |
李四 |
select A.*,B.* from A left outer join B on(A.a1=B.a2)
| a1 |
b1 |
c1 |
a2 |
b2 |
| 01 |
數學 |
90 |
01 |
張三 |
| 02 |
語文 |
80 |
02 |
李四 |
| 03 |
英語 |
70 |
NULL |
NULL |
select A.*,B.* from A right outer join B on(A.a1=B.a2)
| a1 |
b1 |
c1 |
a2 |
b2 |
| 01 |
數學 |
90 |
01 |
張三 |
| 02 |
語文 |
80 |
02 |
李四 |
| NULL |
NULL |
NULL |
04 |
王五 |
select A.*,B.* from A full outer join B on(A.a1=B.a2)
| a1 |
b1 |
c1 |
a2 |
b2 |
| 01 |
數學 |
90 |
01 |
張三 |
| 02 |
語文 |
80 |
02 |
李四 |
| 03 |
英語 |
70 |
NULL |
NULL |
| NULL |
NULL |
NULL |
04 |
王五 |
2、分組 : Group by
一張表,一旦分組完成後,查詢後只能得到組相關的資訊:(統計資訊) count,sum,max,min,avg 分組的標準)
select invest.projectId projectId, invest.projectName projectName, company.companyName companyName, invest.investTime investTime, invest.investMoney investMoney, SUM(invest.investMoney) projectMoney, CONCAT(company.area,company.industryId) projectIntroduction FROM
invest JOIN company ON invest.companyId = company.companyId WHERE fundId= 1 GROUP BY projectId
註:CONCAT為拼接字串函數。
mysql常用sql及常用命令