当前位置:Gxlcms > 数据库问题 > mysql常用sql及常用命令

mysql常用sql及常用命令

时间:2021-07-01 10:21:17 帮助过:3人阅读

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:

a2 b2
01 张三
02 李四
04 王五

 

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及常用命令

标签:

人气教程排行