本次复习按照菜鸟教程文档学习,原地址:https://www.runoob.com/sql/sql-between.html
一.SQL简介
1.RDBMS
2.注意事项
二.SQL基础语法复习
1.数据库表
show DATABASES
use demo
SELECT * FROM EMP_INFO;
create table Websites ( Id INTEGER not null PRIMARY KEY auto_increment, Name VARCHAR(50), Url TEXT, Alexa INTEGER, Country VARCHAR(50) )
INSERT INTO 表名称(列名1,列名2,....) VALUES (值1, 值2,....)
insert into websites(name,url,alexa,country) values('google','https://google.com',1,'USA') insert into websites(name,url,alexa,country) values('淘宝','https://www.taobao.com/',13,'CN') insert into websites(name,url,alexa,country) values('百度','https://baidu.com/',4,'CN') insert into websites(name,url,alexa,country) values('Facebook','https://facebook.com',3,'USA')
2.基本SQL命令展示
- SELECT
- UPDATE
- DELETE
- INSERT INTO
- CREATE DATABASE
- ALTER DATABASE
- CREATE TABLE
- ALTER TABLE
- DROP TABLE
- CREATE INDEX
- DROP INDEX
3.SELECT语句
SELECT * FROM Websites;
SELECT name,country FROM Websites;
4.SELECT DISTINCT语句
SELECT DISTINCT column_name,column_name FROM table_name;
SELECT DISTINCT country FROM Websites;
5.SQL WHERE 子句
SELECT column_name,column_name FROM table_name
WHERE column_name operator value;
SELECT * FROM Websites WHERE country='CN';
= |
等于 |
<> |
不等于。注释:在 SQL 的一些版本中,该操作符可被写成 != |
> |
大于 |
< |
小于 |
>= |
大于等于 |
<= |
小于等于 |
BETWEEN |
在某个范围内 |
LIKE |
搜索某种模式 |
IN |
指定针对某个列的多个可能值 |
6.SQL AND & OR 运算符
(1)AND
SELECT * FROM Websites WHERE country='CN' AND alexa > 5;
(2)OR
SELECT * FROM WebsitesWHERE country='USA'OR country='CN';
(3)结合
SELECT * FROM Websites
WHERE alexa > 10 AND (country='CN' OR country='USA');
7.SQL ORDER BY 关键字
SELECT column_name,column_name FROM table_name ORDER BY column_name,column_name ASC|DESC;
(1)降序排序
SELECT * FROM Websites ORDER BY alexa DESC;
(2)升序排序
(3)结合
SELECT * FROM Websites ORDER BY country asc,alexa desc;
8.SQL INSERT INTO 语句
INSERT INTO table_name VALUES (value1,value2,value3,...);
INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
insert into websites(name,url,alexa,country) values('google','https://google.com',1,'USA')
9.SQL UPDATE 语句
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
UPDATE Websites SET alexa='123', country='CN'
WHERE name='淘宝';
UPDATE Websites SET alexa='123', country='CN'
10.SQL DELETE 语句
DELETE FROM table_name WHERE some_column=some_value;
DELETE FROM Websites
WHERE name='Facebook' AND country='CN';