win10下
初学配合sqlyog可视化工具使用,方便理解。在界面中操作时,也可以查看到对应的脚本代码
MYSQL安装教程,建议配置环境变量
sqlyog网上下载一个破解版即可。
0.基本命令
进入mysql后,每段代码以;结尾才会执行,直接按回车不执行。mysql代码不区分大小写,大写为了视觉强调(或者可以考虑命令用大写,变量用小写,自己习惯即可)
mysql -u root -p 通过cmd进入mysql
set password for root@localhost = password('123'); 更改密码(@后为用户名,本地服务器一般就是localhost,引号内为密码)
source c:\mysql\populate.sql 执行一个已经编写好的脚本文件。(这里导入了SQL必知必会教程内的内容,后面以此为例)
show databases; 返回可用的数据库
use aaa;选择一个数据库,之后的操作便在该数据库下进行
show tables; 返回数据库内的列表

show columns from customers; 展示其中一个表的结构
简写方式:describe customers;

1、检索命令
select prod_name from products; 展示pruducts表中,prod_name中的元素,展示多列时用逗号隔开
select prod_name,others from products;
select * from products;
select distinct vend_id from products; 只返回不同值
select prod_name from products limit 5;只展示5个
select prod_name from products limit 3,4; 展示从行3开始的4个
select prod_name from products limit 4 offset 3;

2、排序
select prod_name from products order by prod_name;默认以字母顺序(ASCII码?)排序
后面加desc表示降序

select prod_id , prod_price,prod_name from products order by prod_price,prod_name;
选择多组数据以多个指标进行排序(price相同情况下,再根据name排序)

3、数据过滤
select prod_name,prod_price from products where prod_price=2.50;
where表示指定的过滤条件,放在from之后,order之前
大小判断符号与一般编程语言相通,表示两者之间用between
select prod_name,prod_price from products where prod_price<=10;
select vend_id,prod_name from products where vend_id != 1003;
select prod_name,prod_price from products where prod_price between 5 and 10;
select prod_name from products where prod_price is null;
Null表示空值,含有空格、0等均不是空值
可以通过and or in not语句组合where条件,其中and优先级高于or
select prod_name,prod_price from products where (vend_id=1002 or vend_id=1003)and prod_price >=10;

为了消除阅读歧义,通常即使顺序符合优先级,也要添加括号
select prod_name,prod_price from products where vend_id in(1002,1003) order by prod_name;
in的使用比Or更加灵活,执行速率也更快
select prod_name,prod_price from products where vend_id not in(1002,1003) order by prod_name;
----------------------------------------------------------------------------------------------------------
通配符:用来匹配值的特殊字符
like关键词用于引出通配符比较
%用于指代任意个字符
_用于指代1个字符
select prod_id,prod_name from products where prod_name like 'jet%';
表示查找以jet开头的任意词

select prod_id,prod_name from products where prod_name like '%anvi%';
表示任意位置包含anvi
%可以指代0~任意个字符,注意表示后缀时,尾部若有空格会干扰查找
select prod_id,prod_name from products where prod_name like'_ ton anvil';

通配符搜索效率较低
----------------------------------------------------------------------------------------------------------
正则表达式:用一组“规则字符串”来表达对字符串的逻辑过滤
select prod_name from products where prod_name regexp '.000' order by prod_name;
.在正则表达式中表示一个字符,与通配符_类似
或的正则表达式
select prod_name from products where prod_name regexp '1000|2000' order by prod_name;
select prod_name from products where prod_name regexp '[123] ton' order by prod_name;

[123]等同于[1|2|3],其他的正则表达式有[^123]表示否定,[1-3]表示范围
| . ^这些字符被用作正则表达式,若查找的内容刚好需要包含这些字符,需要用\\引导
select vend_name from vendors where vend_name regexp '\\.' order by vend_name;

匹配指定字符集:

指定匹配数目:

定位符:

4、数据组合
将一些存储在不同列中的数据拼接成我们需要的数据
select concat(vend_name,'(',vend_country,')') from vendors order by vend_name;

concat用于拼接不同列的内容,同时可以用单引号添加自定义的字符串
trim函数用于去空格,Rtrim表示去掉右边的空格,ltrim同理。
注意,拼接后的字段没有名字,无法被引用,可以用as 赋予别名
select concat(rtrim(vend_name),'(',rtrim(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=20005;
同样也可以利用加减乘除,对表中值为数字的进行算术运算
---------------------------------------------------------------------------------------------------------
使用函数:优点,方便;缺点,移植性差


日期的处理:

日期的标准格式:yyyy-mm-dd
select cust_id,order_num from orders where date(order_date)='2005-09-01';
注:即使order_data中包含了除日期外的其他信息,如时间等,也能检索出来,直接用匹配则不行
select cust_id,order_num from orders where year(order_date)=2005 and month(order_date)=9;
数值处理函数:

1264

被折叠的 条评论
为什么被折叠?



