MYSQL——《SQL必知必会》学习笔记

2017/12/4周一

一天看了一半的图灵程序设计丛书的这本《SQL必知必会》第4版,数据库入门经典书

平台:Windows下的命令行操作

数据库:MYSQL


1.安装会出现的问题

【解决方案】MySQL-5.7.9 服务无法启动-“NET HELPMSG 3534”

http://blog.csdn.net/i_am_wangbo/article/details/49999407

解决方法:删除data文件夹,重新初始化


2.参考的是易百教程网站的MySQLhttp://www.yiibai.com/mysql/



下面是书的前一半记录的笔记:

show databases;

use mysql1;
show tables;
select * from products;
####注释方法1:任意位置
--注释方法2:得出现在命令行 行后
/*注释方法3:任意位置*/


##第二章 检索数据 
--显示全部 ‘*’
select * from products;
--显示3列,逗号‘,’隔开
select prod_name, vend_id, prod_price from products;


--显示1列 
select prod_name from products;
--只显示5行 ‘limit 5’
select prod_name from products LIMIT 5;
--显示2行后的3行,即第3行到第5行 ‘limit 3 offset 2’或 ‘LIMIT 2,3’
select prod_name from products LIMIT 3 OFFSET 2;
select prod_name from products LIMIT 2,3;




--显示不重复的不同的值 ‘DISTINCT’
select DISTINCT vend_id from products;


##第三章 排序检索数据 “ORDER by prod_price DESC”
--按照‘vend_id’排序  “ORDER by prod_price DESC”(默认升序ASC(ascending)
--ASC  a  scending 递增
--DESC de scending 递减
select prod_name, vend_id, prod_price from products ORDER by prod_price;
select prod_name, vend_id, prod_price from products ORDER by prod_price, prod_name;
select prod_name, vend_id, prod_price from products ORDER by prod_price DESC, prod_name;


##第四章 过滤数据 "WHERE prod_price = 3.49" BETWEEN 5 and 10
select prod_id, prod_name, prod_price from products WHERE prod_price = 3.49;
select prod_id, prod_name, prod_price from products WHERE prod_price < 10;
select prod_id, prod_name, prod_price from products WHERE prod_price BETWEEN 5 and 10;
--ORDER应该在WHERE之后
select prod_id, prod_name, prod_price from products WHERE prod_price < 10 ORDER by prod_price DESC;


--IS NULL判断是否为NULL值
select * from customers where cust_email IS NULL;


--AND ,字符串只用单引号‘’表示
select prod_id, vend_id, prod_name, prod_price from products 
WHERE prod_price < 10  AND  vend_id = 'DLL01';
--OR
select prod_id, vend_id, prod_name, prod_price from products 
WHERE prod_price < 5  or vend_id = 'FNG01';
--AND OR混用/ AND优先级更高,使用时最好加括号 / 注意以下2句的区别
select prod_id, vend_id, prod_name, prod_price from products 
WHERE vend_id = 'DLL01' OR vend_id = 'BRS01' AND prod_price >=10;
select prod_id, vend_id, prod_name, prod_price from products 
(WHERE vend_id = 'DLL01' OR vend_id = 'BRS01') AND prod_price >=10;




#第五章 高级过滤 IN  NOT
--IN vend_id IN ( 'DLL01','BRS01' ) 与or类似,但执行更快,语法更清晰 
select prod_id, vend_id, prod_name, prod_price from products 
WHERE vend_id IN ( 'DLL01','BRS01' ) ORDER BY prod_name;
--NOT NOT vend_id = 'DLL01' 找出与条件不匹配的行 
select prod_id, vend_id, prod_name, prod_price from products 
WHERE NOT vend_id = 'DLL01' ORDER BY prod_name;


#第六章 通配符LIKE  '% _ '  


#注意通配符要用LIKE 不能用 = ,因为不相等
--LIKE  '%'
select prod_id, vend_id, prod_name, prod_price from products 
WHERE vend_id LIKE 'D%' ORDER BY prod_name;
--'_'下划线 只能匹配单个字符
select prod_id, vend_id, prod_name, prod_price from products 
WHERE vend_id LIKE 'D_L01' ORDER BY prod_name;
####################################################################################

####################################################################################


#第7章 创建计算字段
--CONCAT 拼接连接多个数组 ,字符串用单引号包括
select CONCAT(vend_name, ' ','(',vend_country,')')  from vendors;
--AS 为新列命名
select CONCAT(vend_name, ' ','(',vend_country,')') AS result from vendors;
--计算新的一列,命名为result
select prod_id,quantity,item_price, quantity*item_price AS result  from orderitems;
--RTRIM() LTRIM() TRIM()去除字符串的空格


#总结:select为测试、检验、计算提供了很好的方法


#第8章 函数
--UPPER()转换为大写字符串 LOWER()
select vend_name, UPPER(vend_name) as result from vendors;
--YEAR()
select order_num from orders where year(order_date) = 2012;
#总结,函数各数据库软件不一致,移植性不好


#第9章 汇总数据
--聚集函数,各软件都挺一致
AVG() COUNG() MAX() MIN() SUM()


#第10章

你可能感兴趣的:(MYSQL——《SQL必知必会》学习笔记)