mysql学习笔记(11-20)
十一、使用数据处理函数
1.文本处理函数
select vend_name,upper(vend_name) AS vend_name_upcase
from vendors
order by vend_name; //很明显转化为大写
SOUNDEX是一个将任何文本串转换为描述其语音表示的字母数字模式的算法。SOUNDEX考虑了类似的发音字符和音节,使得能对串进行发音比较而不是字母比较。
select cust_name,cust_contact
from customers
where soundex(cust_contact)=soundex('Y Lie');
在这个例子中,WHERE子句使用Soundex()函数来转换cust_ contact列值和搜索串为它们的SOUNDEX值。因为Y.Lee和Y.Lie发音相似,所以它们的SOUNDEX值匹配,因此WHERE子句正确地过滤出了所需的数据。
2.日期和时间处理函数
不管是插入或更新表值还是用WHERE子句进行过滤,日期必须为格式yyyy-mm-dd。
select cust_id,order_num
from orders
where order_date = '2005-09-01';
//上面这种写法不好,应为如果是 2005-09-01 11:30:05 就会匹配失败
解决办法是指示MySQL仅将给出的日期与列中的日期部分进行比较,而不是将给出的日期与整个列值进行比较。为此,必须使用Date()函数。Date(order_date)指示MySQL仅提取列的日期部分,更可靠的SELECT语句为:
select cust_id,order_num
from orders
where date(order_date)= '2005-09-01';
同样,只需要时间的话,用Time()函数。
如果你想检索出2005年9月下的所有订单,怎么办?
//方法一
select cust_id,order_num
from orders
where date(order_date) between '2005-09-01' and '2005-09-30';
//方法二
select cust_id,order_num
from orders
where year(order_date)=2005 and month(order_date)=9;
3.数值处理函数
还没有评论,来说两句吧...