MySQL必知必會-9MySQL常用函式及數學計算
阿新 • • 發佈:2019-01-25
拼接欄位
使用concat()函式來拼接多個列
別名是一個欄位或值的替換名,別名用AS關鍵字賦予。
SELECT可以省略FROM子句以便簡單地訪問和處理表達式。例如:
SELECT 3 * 2; 將返回6
SELECT Trim('abc');將返回abc,而SELECT Now(),利用Now()函式返回當前日期和時間。
可以通過SELECT進行試驗。
使用資料處理函式
Upper()函式將文字轉換成大寫。
SOUNDEX是一個將任何文字串轉換為描述其語音表示的字母數字模式的演算法。 SOUNDEX考慮了類似發音字元和音節,使得能對串進行發音比較而不是字母比較。
使用Soundex()函式匹配類似的發音
(MySQL官方原話:This function, as currently implemented, is intended to work well with strings that are in the English language only. Strings in other languages may not produce reliable results.
This function is not guaranteed to provide consistent results with strings that use multibyte character sets, including utf-8.
We hope to remove these limitations in a future release. See Bug #22638 for more information. 大致意思是:僅對英文有支援,其他語言可能會不可靠,且不能保證對多位元組字符集字串(包括UTF08)提供一致的結果。會在未來取消掉這些限制,詳細看Bug #22638。 經過本人測試,中文的確不可靠,目前來看就是雞肋,以後可能會支援)
SELECT
Concat(
vend_name,
'(',
vend_country,
')',
vend_address
)
FROM
vendors
ORDER BY
vend_name;
SELECT
Concat(
RTrim(vend_name),
'(',
RTrim(vend_country),
')'
)
FROM
vendors
ORDER BY
vend_name;
SELECT
Concat(
RTrim(vend_name),
'(',
RTrim(vend_country),
')'
) AS vend_title
FROM
vendors
ORDER BY
vend_name;
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;
SOUNDEX是一個將任何文字串轉換為描述其語音表示的字母數字模式的演算法。
We hope to remove these limitations in a future release. See Bug #22638 for more information. 大致意思是:僅對英文有支援,其他語言可能會不可靠,且不能保證對多位元組字符集字串(包括UTF08)提供一致的結果。會在未來取消掉這些限制,詳細看Bug #22638。 經過本人測試,中文的確不可靠,目前來看就是雞肋,以後可能會支援)
SELECT
cust_name,
cust_contact
FROM
customers
WHERE
Soundex(CUST_CONTACT)=Soundex('Y Lie');
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
FROM
orders
WHERE
YEAR (order_date)=2005
AND MONTH (order_date)=9;
SELECT
AVG(prod_price) AS avg_price
FROM
products
WHERE
vend_id =1003;
SELECT
COUNT(*) AS num_cust
FROM
customers;
SELECT
COUNT(cust_email) AS num_cust
FROM
customers;
SELECT
COUNT(*),
AVG(quantity),
MAX(item_price),
MIN(item_price),
SUM(quantity * item_price)
FROM
orderitems;
SELECT
AVG(DISTINCT prod_price) AS avg_price
FROM
products
WHERE
vend_id =1003;