MySQL學習(三)

-- 計算字段
-- 拼接字段
SELECT CONCAT(vend_name, ' (',vend_country,')')
FROM Vendors
ORDER BY vend_name;

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

-- CONCAT(str1,str2,...) 拼接查詢的值
SELECT CONCAT(vend_name,vend_country)
FROM Vendors
ORDER BY vend_name;


-- RTRIM(str) 去掉值右邊的全部空格  LTRIM(str) 去掉值左邊的全部空格
SELECT RTRIM(vend_name),RTRIM(vend_country)
FROM Vendors
ORDER BY vend_name;

SELECT RTRIM(vend_name)
FROM Vendors


-- 使用別名 AS
SELECT CONCAT(RTRIM(vend_name),' (',RTRIM(vend_country),')')
AS vend_title
FROM Vendors
ORDER BY vend_name;


-- 執行算術運算
SELECT prod_id,quantity,item_price
FROM OrderItems
WHERE order_num = 20008;

-- 價格彙總
SELECT prod_id,quantity,item_price,quantity * item_price AS expanded_price
FROM OrderItems
WHERE order_num = 20008;


-- 文本處理函數
/*
經常使用文本處理函數
`LEFT`(str,len)        返回字符串左邊的字符
LENGTH(str)                返回字符串的長度
LOWER(str)                將字符串轉換爲小寫
LTRIM(str)                去掉字符串左邊的空格
`RIGHT`(str,len)  返回字符串右邊的字符
RTRIM(str)                去掉字符串右邊的空格
SOUNDEX(str)            返回字符串的soundex值
UPPER(str)                將字符串轉換爲大寫

*/
-- UPPER(str) 將全部字符轉換爲大寫
SELECT vend_name,UPPER(vend_name) AS vend_name_upcase
FROM Vendors
ORDER BY vend_name;

SELECT LEFT(vend_name,1),vend_name
FROM Vendors
ORDER BY vend_name;


-- SOUNDEX(str) 返回讀音相近的結果
SELECT cust_name,cust_contact
FROM Customers
WHERE cust_contact = 'Michael Green';
-- 返回空的結果

SELECT cust_name,cust_contact
FROM Customers
WHERE SOUNDEX(cust_contact) = SOUNDEX('Michael Green');


-- 日期與時間處理函數
SELECT order_num
FROM Orders
WHERE YEAR(order_date) = 2012;


-- 數值處理函數
/*
ABS()  返回一個數的絕對值
COS()  返回一個角度的餘弦
EXP()  返回一個數的指數值
PI()   返回圓周率
SIN()  返回一個角度的正弦
SQRT() 返回一個數的平方根
TAN()  返回一個角度的正切
*/
相關文章
相關標籤/搜索