首页 > 代码库 > mysql-5 数据检索(3)

mysql-5 数据检索(3)

计算字段

  • 如果想在一个字段中既显示公司的名称,又显示公司的地址,但是这两个信息一般包含在不同的表列中
  • 城市、州和邮政编码存储在不同的列中,但是邮件标签打印程序却需要把它们作为一个恰当格式的字段检索出来
  • 物品订单表存储物品的价格和数量,但不需要存储每个物品的总价格,为了打印发票,需要物品的总价
  • 需要根据表数据进行总数、平均数计算或者其它计算

拼接字段

例子:vendors表包含供应商名和位置信息。假如要生成一个供应商报表,需要在供应商的名字中按照name(location)这样的格式列出供应商信息。此报表需要单个值,而表中数据存储在两个列vend_name和vend_country中,此外,需要用括号将vend_country括起来,这些东西都没有明确存储在数据表中,我们来看看怎么样编写返回供应商名和位置的select语句

SELECT CONCAT(vend_name, (,vend_country,)) FROM vendors ORDER BY vend_name;

这里用到了concat函数,拼接串,即把多个串连接起来形成一个较长的串,concat需要一个或者多个指定的串,各个串之间用逗号分隔

技术分享

SELECT CONCAT(RTRIM(vend_name),(,RTRIM(vend_country),)) FROM vendors ORDER BY vend_name;

rtrim函数去掉值右边的所有空格,通过使用rtrim(),各个列都进行了整理

技术分享

使用别名

SELECT CONCAT(RTRIM(vend_id), (,RTRIM(vend_country),)) AS vend_title FROM vendors ORDER BY vend_name;

技术分享

执行算数计算

SELECT prod_id ,quantity ,item_price FROM orderitems WHERE order_num = 20005;

技术分享

SELECT prod_id ,quantity ,item_price, quantity*item_price AS expanded_price  FROM orderitems WHERE order_num = 20005;

技术分享

使用数据处理函数

upper()函数

SELECT vend_name ,UPPER(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name;

技术分享

     函数                                     说明

   Left()                        返回串左边的字符

   Length()                   返回串的长度

   Locate()                   找出串的一个子串

   Lower()                    将串转换为小写

   LTrim()                     去掉串左边的空格

   Right()                     返回串右边的字符

   RTrim()                    去掉串右边的空格

   Soundex()               返回串的soundex值

   SubString()             返回子串的字符

   upper()                    将串转为大写

 

SELECT cust_name , cust_contact FROM customers WHERE SOUNDEX(cust_contact)=SOUNDEX(Y lie);

查找与lie发音相似的cust_contact的列

技术分享

 日期和时间处理函数

 技术分享

SELECT cust_id , order_num FROM orders WHERE order_date = 2005-09-01;
SELECT cust_id , order_num FROM orders WHERE DATE(order_date) = 2005-09-01;

技术分享

SELECT cust_id , order_num ,order_date FROM orders WHERE DATE(order_date) BETWEEN 2005-09-01 AND 2005-09-30;
SELECT cust_id ,order_num,order_date FROM orders WHERE YEAR(order_date)= 2005 AND MONTH(order_date)= 9;

9月份的所有订单

技术分享

SELECT CONCAT(vend_name,  (,vend_country,)) FROM vendors ORDER BY vend_name;
SELECT CONCAT(RTRIM(vend_name), (,RTRIM(vend_country),)) FROM vendors ORDER BY vend_name;
SELECT CONCAT(RTRIM(vend_id), (,RTRIM(vend_country),)) AS vend_title FROM vendors ORDER BY vend_name;
SELECT prod_id ,quantity ,item_price FROM orderitems WHERE order_num = 20005;
SELECT prod_id ,quantity ,item_price, quantity*item_price AS expanded_price FROM orderitems WHERE order_num = 20005;
SELECT vend_name ,UPPER(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name;
SELECT cust_name , cust_contact FROM customers WHERE SOUNDEX(cust_contact)=SOUNDEX(Y lie);
SELECT cust_name , cust_contact FROM customers;
SELECT cust_id , order_num FROM orders WHERE order_date = 2005-09-01;
SELECT cust_id , order_num FROM orders WHERE DATE(order_date) = 2005-09-01;
SELECT cust_id , order_num ,order_date FROM orders WHERE DATE(order_date) BETWEEN 2005-09-01 AND 2005-09-30;
SELECT cust_id ,order_num,order_date FROM orders WHERE YEAR(order_date)= 2005 AND MONTH(order_date)= 9;

 

mysql-5 数据检索(3)