SQL常用语句(大全)

我们先看一下表的结构:

 

-- sql语句创建表

CREATE TABLE student2 (
  id int(11) NOT NULL AUTO_INCREMENT,
  stuname varchar(10) NOT NULL,
  class varchar(20) NOT NULL,
  sex int(11) NOT NULL,
  major varchar(20) NOT NULL,
  mathstore int(11) DEFAULT NULL,
  yuwenstore int(11) DEFAULT NULL,
  engstore int(11) DEFAULT NULL,
  PRIMARY KEY (id)
)

-- sql语句删除表

delete table student2;

-- 添加表字段

Alter table student2 add column historystore int;

-- 删除表字段

alter table student2 drop column historystore;

-- 修改表字段类型、长度

alter table student2 modify column sex char;

-- 查询语句

 select * from student2 where id=5;

-- 删除语句

delete from student2 where id = 5;

-- 修改语句

update  student2 set intime=1249524739 where sex=1;

-- 添加语句

insert into student2(stuname,class,sex,major,mathstore,yuwenstore,engstore) 
values('张四8','三班',1,'计算机',86,99,99);

-- 日期格式化函数

Select from_unixtime(intime) from student2 where sex=1;

-- 字符串函数(分割,类型转换)

select LEFT(stuname,1) as leftsub ,RIGHT(stuname,2) as rightsub,SUBSTRING(stuname,2,3) as sub from student2;
select   * from student2 where CONVERT(INT,hisstore)=41;

-- 求和函数

select sum(yuwenstore) as 二班语文成绩 from student2 where class='二班';

-- if语句

SELECT SUM( IF(mathstore > engstore ,1 ,0) ) AS mannum,
        SUM( IF(mathstore > engstore AND sex > 1, 1 ,0) ) AS womannum 
        FROM student2;

-- select 嵌套查询

SELECT stuname  
FROM Student2`student2`
WHERE id IN
    (SELECT id  
     FROM student2
     WHERE class='一班');

 -- 内连接

 SELECT   student2.*,student.*   
    FROM  student2 INNER   JOIN   student     
    ON   student.stuname=student2.stuname;   

  -- 左连接 

SELECT   student2.*,student.*   
    FROM  student2 LEFT   JOIN   student     
    ON   student.stuname=student2.stuname;

     -- 右连接

SELECT   student2.*,student.*   
    FROM  student2 RIGHT   JOIN   student     
    ON   student.stuname=student2.stuname;     

 -- 全连接

SELECT   student.*,student.*   
    FROM  student   FULL JOIN   student     
    ON   student.stuname=student.stuname;      

 -- 创建索引
 

CREATE INDEX SClass ON student2 (class);

 

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值