1 日期格式化
mysql>SELECT DATE_FORMAT( t, '%M %e, %Y' ), srcuser, size FROM mail;
(设置日期别名)
mysql>SELECT DATE_FORMAT( t, '%M %e, %Y' ) AS 'Date of Message' , srcuser, size FROM mail;
2 使用CONCAT()函数合并多列来构建复合值
mysql> SELECT
>DATE_FORMAT( t, '%M %e, %Y' ) AS date_sent,
> CONCAT (srcuser,'@', srchost) AS sender,
> CONCAT (dstuser, '@' , dsthost) AS recipient,
> size FROM mail;
3 WHERE 表达式中的列别名
mysql> SELECT t, srcuser, dstuser, size/1024 AS kilobytes
> FROM mail WHERE size/1024>500;
4 DISTINCT使查询结果唯一化(同时使用COUNT()来统计数量)
在查询的结果中,同一信息可能出现多次,使用DISTINCT去掉重复项
mysql> SELECT DISTINCT srcuser FROM mail;
使用COUNT()来计数:
mysql> SELECT COUNT( DISTINCT srcuser ) FROM mail;
5 关于NULL
查找等于NULL的列值:
mysql> SELECT * FROM taxpayer WHERE id = NULL;
使用<=>来比较两个NULL值
mysql> SELECT NULL = NULL, NULL <=> NULL;--------将返回NULL 和 1;
使用Unknown来代替Null值
mysql> SELECT name , IF( id IS NULL, 'Unknow', id ) AS 'id' FROM taxpayer;
mysql>SELECT name , IFNULL( id, 'Unknow' ) AS 'id' FROM taxpayer;