SQL语句表操作

Posted wjwap

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句表操作相关的知识,希望对你有一定的参考价值。

创建表
create table student(
    id int not null auto_increment primary key comment ‘这是主键‘,
    sno varchar(10) comment ‘这是姓名‘,
    sex int default 1 comment ‘这是性别 1代表男 0代表女‘,
    brithday date
);
数据类型: int date datetime  timestemp  varchar(20)
查看表结构
desc student;
插入数据
insert into score(sno,cno,degree) values
(‘103‘,‘3-245‘,86);

delete from student where ...

update student set ssex="女" where...

select sno from student where ...
group  by  字段1
连接查询:扩充字段
Select * from student,teacher;
子查询:把查询结果当条件
Select sno,cno from score where degree=(select max(degree) from score);
联合查询
Select sno,sname from student union select tno,tname from teacher;

添加字段
ALTER TABLE order01 ADD COLUMN odesc VARCHAR(50);
删除字段
ALTER TABLE order01 DROP COLUMN money;
修改字段类型
ALTER TABLE order01 MODIFY COLUMN otime VARCHAR(20);
修改字段名称
ALTER TABLE order01 CHANGE COLUMN otime ordertime VARCHAR(20);
修改表名
ALTER TABLE order01 RENAME TO order02;
修改数据
UPDATE order02 SET ordertime=‘2018-12-12‘ WHERE oid=2;
删除数据
DELETE FROM order02 WHERE oid=2;
全表删除
DELETE FROM order02;
TRUNCATE TABLE order02;
查询所有字段
SELECT*FROM order02;
查询指定字段
SELECT oid,productname,odesc FROM order02;
查询时添加常量列
SELECT oid AS‘编号‘,productname FROM order02;

sql语句例子
新建成绩表
CREATE TABLE score(
    sid INT,
    sname VARCHAR(10),
    ssex CHAR(2),
    html DOUBLE,
    js DOUBLE,
    jquery DOUBLE
);
DROP TABLE score;
查询时合并列
SELECT sname,(html+js+jquery) AS ‘总成绩‘ FROM score;
查询时去除重复列
SELECT DISTINCT ssex FROM score;
SELECT DISTINCT(ssex) FROM score;
逻辑条件查询
SELECT * FROM score WHERE sid=1 AND ssex=‘男‘;
SELECT * FROM score WHERE sid=2 OR ssex=‘女‘;
比较条件查询
SELECT * FROM score WHERE html>=60 AND js<=80;
SELECT * FROM score WHERE html>=60 AND html<=100;
SELECT * FROM score WHERE html BETWEEN 60 AND 100;
SELECT * FROM score WHERE <>60 AND js>8;
判断是否为null
SELECT * FROM score WHERE js IS NOT NULL;
判断是否为空字符串
SELECT * FROM score WHERE ssex <> ‘‘;
SELECT * FROM score WHERE ssex = ‘‘;
模糊查询
SELECT * FROM score WHERE sname LIKE ‘张%‘;
SELECT * FROM score WHERE sname LIKE ‘__‘;
SELECT * FROM score WHERE sname LIKE ‘%张%‘;
SELECT SUM(js) AS ‘js成绩‘ FROM score;
SELECT AVG(html) AS ‘html平均成绩‘ FROM score;
SELECT MAX(jquery) FROM score;
SELECT MIN(js) FROM score;
求本班多少人
SELECT COUNT(*) FROM score;
第一页
SELECT * FROM score LIMIT 0,1;
第二页
SELECT * FROM score LIMIT 2,1;
查询排序
SELECT * FROM score ORDER BY html ASC;
SELECT * FROM score WHERE ssex=‘男‘ ORDER BY html DESC;
SELECT * FROM score ORDER BY js ASC,jquery DESC;
分组查询
SELECT ssex,COUNT(*) FROM score GROUP BY ssex;
查询总人数大于2的性别(HAVING)
按性别分组
SELECT ssex FROM score GROUP BY ssex
HAVING COUNT(*)>2;








































































































以上是关于SQL语句表操作的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句表操作

MySQL的SQL语句 - 数据操作语句(13)- 子查询(10)

用sql语句怎么查一个表的信息

MySQL的SQL语句 - 数据操作语句(13)- 子查询

7. SQL—创建和操作表

MySQL的SQL语句 - 数据操作语句(15)- UPDATE 语句