Mysql sql语句大全

1、说明:创建数据库   create database  dbname;

2、说明:删除数据库   drop database dbname;

3、说明:创建新表      create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..);

4、说明:删除新表  drop table tabname;

5、说明:增加一个列   Alter table tabname add column col type;

6、说明:几个简单的基本的sql语句
  选择:select * from table1 where 范围;
  插入:insert into table1(field1,field2) values(value1,value2);
  删除:delete from table1 where 范围;
  更新:update table1 set field1=value1 where 范围;
  查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!;
  排序:select * from table1 order by field1,field2 [desc];
  总数:select count as totalcount from table1;
  求和:select sum(field1) as sumvalue from table1;
  平均:select avg(field1) as avgvalue from table1;
  最大:select max(field1) as maxvalue from table1;
  最小:select min(field1) as minvalue from table1;

7、左连接:

  左连接即:返回左边表中所有被查询字段+右边表中符合条件的字段。  

select stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu LEFT JOIN tech on stu.classe_name=tech.classe_name;

 

  下面是两张表:

  表stu

  

  表tech

  

  结果如下:

8、内连接:

  返回表中符合条件的条目。

SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu INNER JOIN tech on stu.classe_name=tech.classe_name;

结果如下:

9、右连接:
  返回右边表中所有被查询字段+左边表中符合条件的字段。
SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu RIGHT JOIN tech on stu.classe_name=tech.classe_name;

  结果如下: