|
第04天
1.主题:系统学习mysql
《MySQL必知必会》—Ben Forta著
2.摘要
多数正则表达式实现使用单个反斜杠转义特殊字符, 以便能使用这些字符本身。但MySQL要求两个反斜杠(MySQL 自己解释一个,正则表达式库解释另一个)。
select urlname from urls where urlname regexp '^[wiki\\.]’;定位符^ $的运用;
LIKE和REGEXP 的不同在于,LIKE匹配整个串而REGEXP匹配子串。利用定位 符,通过用^开始每个表达式,用$结束每个表达式,可以使 REGEXP的作用与LIKE一样。
select concat(id,'(',urlname,')') from urls limit 9;Concat()拼接串,即把多个串连接起来形成一个较长的串。
Concat()需要一个或多个指定的串,各个串之间用逗号分隔。
select concat(id,'(',urlname,')') as newname from urls limit 9;使用别名;
select id * 100 as newid from urls where id < 12;
select upper(urlname) as NAMES from urls;利用函数upper()返回大写;
不管是插入或更新表值还是用WHERE子句进行过滤,日期必须为 格式yyyy-mm-dd;
select now();返回当前时刻;
select avg(length(urlname)) as avglen from urls where id between 100 and 200;求取平均值;
select count(*) as ctotal from urls;统计所有行记录数,包括空值行;
select count(urlname) as ctotal from urls;统计特定列的行记录数,不包括空值行;
select distinct length(urlname) as len from urls;只包含不同的值,指定DISTINCT参数。
select avg(distinct length(urlname)) as avglen from urls where id between 100 and 200;
select length(urlname) as lename, count(*) as totalnum from urls group by length(urlname);分组数据;
GROUP BY子句必须出现在WHERE子句之后,ORDER BY子句之前。
WHERE过滤行,而HAVING过滤分组。 WHERE在数据 分组前进行过滤,HAVING在数据分组后进行过滤。这是一个重 要的区别。比如:
select length(urlname), count(*) as totalnum from urls where id >300 group by length(urlname) having count(*)>20;
关键是,相同数据出现多次决不是一件好事,此因素是关系数据库 设计的基础。关系表的设计就是要保证把信息分解成多个表,一类数据 一个表。各表通过某些常用的值(即关系设计中的关系(relational))互 相关联。
外键(foreignkey) 外键为某个表中的一列,它包含另一个表 的主键值,定义了两个表之间的关系。
select details.name, address from details,tester where details.name = tester.name order by details.name;用完全限定列名创建联结查询,where语句正确过滤很重要;
select details.name, address from details inner join tester on details.name = tester.name order by details.name;内部联结inner join on是标准语法,与from where一样做等值测试;
select d.name, address from details as d ,tester as t where d.name = t.name order by d.name;使用表别名做简化;
在使用OUTER JOIN语法时,必须使用RIGHT或LEFT关键字 指定包括其所有行的表(RIGHT指出的是OUTER JOIN右边的表,而LEFT 指出的是OUTER JOIN左边的表)。
select name,sex from tester union select name,sex from details;组合查询union前后的select必须列数相同;
select name,sex from tester union all select name,sex from details;组合查询的union去重,但union all不去重;
select name,sex from tester union all select name,sex from details order by name;组合查询的排序必须在最后写上order by;
3.心得感悟
A.进展很快,这书不错,言简意赅,需要大量练习;
B.主键、外键要加强了解;
4.时间统计
昨日阅读5小时,累计265小时
|