SQL常用语句

时间:2019-09-08
本文章向大家介绍SQL常用语句,主要包括SQL常用语句使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

常用的一些SQL语句

use test ;  -- 使用test数据库
show TABLES;  -- 查询test数据库中的表格
use student; -- 使用student表格
SELECT*FROM student; -- 查询student表格

-- 插入学生信息
INSERT into student ( name, age ,chinese) VALUES('张三丰', 29,99),
INSERT into student ( name, age ,chinese,math,english) VALUES('薛之谦', 28,85,99,65),
INSERT into student ( name, age ,chinese,math,english) VALUES('小胖伟', 28,66,99,89),
INSERT into student ( name, age ,chinese,math,english) VALUES('花花花', 28,56,35,99),
INSERT into student ( name, age ,chinese,math,english) VALUES('张无忌', 28,85,99,57),
INSERT into student ( name, age ,chinese,math,english) VALUES('慕容云海', 28,84,99,84);

 -- 删除 id大于15的学生
DELETE from student WHERE id>15;

-- 查询student表中的name和age
SELECT name, age from student;


SELECT 
        name,  -- 姓名
        age,   -- 年龄
        english -- 英语成绩
FROM 
      student;  -- 学生表
        
        
SELECT name ,address from student;
--  去除重复结果集
SELECT DISTINCT address from student;
        
-- 计算三科成绩之和
SELECT name, address,chinese,math,english,chinese+math+english from student;
        
SELECT name, address,ifnull(chinese,0)语文,IFNULL(math,0)数学,IFNULL(english,0)英语,ifnull(chinese,0)+IFNULL(math,0)+IFNULL(english,0)总分 from student;

SELECT * FROM student where age= 23;

-- 查询年龄在20-30之间的学生
SELECT * FROM student where age BETWEEN 20 AND 30;

--  查询年龄为20,21,22,23的小伙伴
SELECT * FROM student where age in (20,21,22,23);

-- 查询英语成绩为null的小伙伴
SELECT * FROM student where english= null;-- 由于null是特殊的,不能用=(!=)来判断
-- 下面这个是正确的
SELECT * FROM student where english is null;

-- 查询英语成绩不为null的小伙伴
SELECT * FROM student where english is  not null;


-- 查询姓薛的有哪些
SELECT * FROM student WHERE name LIKE '薛%';

-- 查询第二个字是之的人
SELECT * FROM student WHERE name LIKE '_之%';


-- 查询姓名是四个字的人
SELECT * FROM student WHERE name LIKE '____';-- 此处四个_


-- 查询姓名含有'云'的人
SELECT * FROM student WHERE name LIKE '%云%';

原文地址:https://www.cnblogs.com/SilenceRui/p/11488224.html