日期:2014-05-16 浏览次数:20987 次
1. 创建计算字段 拼接字段 select concat(vend_name, '(', vend_country, ')') as name from vendors order by vend_name; 删除数据右侧或左侧多余的空格 select concat(RTrim(vend_name), '(', RTrim(vend_country), ')') as name from vendors order by vend_name; 2. 执行算数计算 select prod_id, quantity, item_price, -> quantity*item_price as expanded_price from orderitems where order_num = 20005; +---------+----------+------------+----------------+ | prod_id | quantity | item_price | expanded_price | +---------+----------+------------+----------------+ | ANV01 | 10 | 5.99 | 59.90 | | ANV02 | 3 | 9.99 | 29.97 | | TNT2 | 5 | 10 | 50.00 | | FB | 1 | 10 | 10.00 | +---------+----------+------------+----------------+ select now(); select trim('ada'); 3. 文本处理函数 select vend_name, upper(vend_name) as vend_name_upcase from vendors order by vend_name; +----------------+------------------+ | vend_name | vend_name_upcase | +----------------+------------------+ | ACME | ACME | | Anvils R Us | ANVILS R US | | Furball Inc. | FURBALL INC. | | Jet Set | JET SET | | Jouets Et Ours | JOUETS ET OURS | | LT Supplies | LT SUPPLIES | +----------------+------------------+ Soundex 的使用,发音的匹配 select cust_name, cust_contact from customers where soundex(cust_contact) = soundex('Y Lie'); +-------------+--------------+ | cust_name | cust_contact | +-------------+--------------+ | Coyote Inc. | Y Lee | +-------------+--------------+ 4. 日期和时间处理函数 AddDate() 增加一个日期 AddTime() 增加一个时间 CurDate() 返回当前日期 CurTime() 返回当前时间 Date() 返回日期时间的日期部分 DateFiff() 计算两个日期之差 Date_Add() 高度灵活的日期运算函数 Date_Format() 返回一个格式化的日期或时间串 Day() 返回一个日期的天数部分 DayOfWeek() 对于一个日期,返回对应的星期 Hour() 返回时间的小时部分 Minute() 返回一个时间的分钟部分 Month() 返回一个日期的月份部分 Now() 返回当前日期和时间 Second() 返回一个时间的秒部分 Time() 返回一个日期时间的时间部分 Year() 返回一个日期的年份部分 select cust_id, order_num from orders where order_date = '2005-09-01'; Datetime 类型只匹配日期 2005-09-01 11:30:05 select cust_id, order_num from orders where Date(order_date) = '2005-09-01'; 检索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; select cust_id, order_num from orders -> where Date(order_date) >= '2005-09-01' and Date(order_date) <= '2005-09-30'; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10003 | 20006 | | 10004 | 20007 | +---------+-----------+ 5. 聚集函数 运行在行组上,计算和返回单个值的函数 AVG() 返回某列的平均值 COUNT() 返回某列的行数 MAX() 返回某列的最大值 MIN() 返回某列的最小值 SUM() 返回某列之和 AVG() 忽略NULL 值的行 select AVG(prod_price) as avg_price from products; +-----------+ | avg_price | +-----------+ | 16.133571 | +-----------+ COUNT() COUNT(*) 对表中行的数目进行计数,不管表列中包含的是空(NULL),还是非空值。 COUNT(column)对表中特定劣种具有值的进行计数,忽略NULL 值。 select count(cust_email) as num_cust from customers; +----------+ | num_cust | +----------+ | 3 | +----------+ MAX() MIN() select max(prod_price) as max_price, prod_name from products; +-----------+--------------+ | max_price | prod_name | +-----------+--------------+ | 55 | .5 ton anvil | +-----------+--------------+ SUM() select sum(quantity) as items_ordered from orderitems where order_num = 20005; +---------------+ | items_ordered | +---------------+ | 19 | +---------------+ SUM 也可以用来合计计算值 select sum(quantity*item_price) as total_price from orderitems where order_num = 20005; +-------------+ | total_price | +-------------+ | 149.87 | +-------------+ 聚集不同值 DISTINCT select AVG(distinct prod_price) as avg_price from products where vend_id = 1003; +-----------+ | avg_p