数据库
首页 > 数据库> > SQL必知必会笔记(一)

SQL必知必会笔记(一)

作者:互联网

SQL必知必会(一)

1,任何人一直学习任何一门手艺都会有些烦躁
2,休息好和玩好是学好的前提
3,死记硬背是融会贯通的前提

基础知识

数据:

数据库:保存有组织的数据的容器(通常是一个文件或一组文件)

表:某种特定类型数据的结构化清单 表名唯一

数据类型

行:记录

主键:

外键:

必备代码



#一切以mysql为演示

#切换表

USE dingdan;

#检索单个列

SELECT prod_name FROM products;

#检索多个列

SELECT prod_name,prod_id,prod_price FROM
products;

#检索所有列

SELECT * FROM products;

#只保留不同的值

SELECT DISTINCT vend_id FROM products;

#限制结果(MYSQL)

SELECT prod_name FROM products LIMIT 5; //前5个

SELECT prod_name FROM products LIMIT 3
OFFSET 5; //第三行起的5行数据

#排序数据(单行)

SELECT prod_name FROM products

ORDER BY prod_name;

#排序数据(多行)

SELECT prod_name,prod_id,prod_price

FROM products

ORDER BY prod_id,prod_name;

#排序数据(按照列的位置)

SELECT prod_name,prod_id,prod_price

FROM products

ORDER BY 2,1;

#指定排序方向(降序)

SELECT prod_name,prod_id,prod_price

FROM products

ORDER BY prod_id,prod_name DESC;

#指定排序方向(升序)默认升序

SELECT prod_name FROM products

ORDER BY prod_name ASC;

#过滤数据  = 
<>  !=  < > <= >= !< !> between is
null

SELECT prod_name,prod_price

FROM products

WHERE prod_price = 3.49;

#检查单个值

SELECT prod_name,prod_price

FROM products

WHERE prod_price > 3.49;

#不匹配检查

SELECT prod_name,prod_price

FROM products

WHERE prod_price <> 3.49;

#between

SELECT prod_name,prod_price

FROM products

WHERE prod_price BETWEEN 5 AND 10;

#空值检查

SELECT prod_name,prod_price

FROM products

WHERE prod_price IS NULL;

#组合where  and

SELECT prod_name,prod_price

FROM products

WHERE prod_price BETWEEN 5 AND 10

AND 

WHERE prod_name IS NOT NULL;

#or

SELECT prod_name,prod_price

FROM products

WHERE vend_id = 'DLL01' OR vend_id =
'BRS01';

#SQL优先处理and,再处理or

SELECT prod_name,prod_price

FROM products

WHERE(vend_id = 'DLL01' OR vend_id =
'BRSO1')

AND prod_price >=10;

#IN 操作符

SELECT prod_name,prod_price

FROM products

WHERE vend_id IN ('DLL01','BRS01')

ORDER BY prod_name;

#NOT 否定紧跟的限定语句

SELECT prod_name,prod_price

FROM products

WHERE NOT vend_id = 'DLL01'

ORDER BY prod_name;

#通配符 %

SELECT prod_id,prod_name

FROM Products

WHERE prod_name LIKE 'Fish%';

 

SELECT prod_id,prod_name

FROM Products

WHERE prod_name LIKE '%bean bag%';

# _ 单个字符

SELECT prod_id,prod_name

FROM Products

WHERE prod_name LIKE '__ inch teddy bear';

#[] 字符集匹配

SELECT cust_contact

FROM Customers

WHERE cust_contact LIKE '[JM]%'

ORDER BY cust_constact;

#[^] 字符集匹配

SELECT cust_contact

FROM Customers

WHERE cust_contact LIKE '[^JM]%'

ORDER BY cust_constact;

#拼接(MySQL)

SELECT CONCAT(vend_name,'
(',vend_country,')')

FROM Vendors

ORDER BY vend_name;

#RTRIM 去除右边空格

#LTRIM 去除左边空格

#TRIM 去除左右空格 

SELECT RTRIM(vend_name)+'(' +
RTRIM(vend_country) + ')'

FROM Vendors

ORDER BY vend_name;

#使用别名(MySQL)

SELECT CONCAT(vend_name,'
(',vend_country,')') AS vend_title

FROM Vendors

ORDER BY vend_name;

#执行算术计算

SELECT prod_id,quantity,item_price,

quantity * item_price AS expanded_price

FROM OrderItems

WHERE order_num = 20008;

#函数 UPPER

SELECT vend_name, UPPER(vend_name) AS
vend_name_upcase

FROM Vendors

ORDER BY vend_name;

/*

UPPER()  
转换成大写

LOWER()         小写

LEFT()   
返回字符串左边的字符

RIGHT()             右

LENGTH() 
返回字符串的长度

SOUNDEX() 返回字符串的语音表示的字母数字

*/

#发音匹配

SELECT cust_name,cust)contact

FROM Customers

WHERE SOUNDEX(cust_contact) =
SOUNDEX('Michael Green');

#日期函数(MySql)

SELECT order_num

FROM Orders

WHERE YEAR(order_date) = 2012;

 

/*

ABS()绝对值

COS()余弦

EXP()指数值

PI()圆周率

SIN()正弦

SQRT()平方根

TAN()正切

AVG()

COUNT()

MAX()

MIN()

SUM()

*/

 

#AVG()

SELECT AVG(prod_price) AS avg_price

FROM Products

WHERE vend_id = 'DLL01';

#count()

SELECT COUNT(cust_email) AS num_cust

FROM Customers;

#组合聚集函数

SELECT COUNT(*) AS num_items,

MIN(prod_price) AS price_min,

MAX(prod_price) AS price_max,

AVG(prod_price) AS price_avg

FROM Products;

#数据分组

SELECT vend_id,COUNT(*) AS num_prods

FROM Products

GROUP BY vend_id;

#过滤分组

SELECT vend_id,COUNT(*) AS num_prods

FROM Products

GROUP BY vend_id

HAVING COUNT(*) >=2;

今天下午SQL学到这里算是烦躁了,明天加油!

悦悦是个大菜鸟 发布了9 篇原创文章 · 获赞 6 · 访问量 3468 私信 关注

标签:name,必知,price,SQL,vend,必会,prod,id,SELECT
来源: https://blog.csdn.net/zhangzhangchenchu/article/details/104100215