標籤:
1.建立計算欄位:
concat , select concat(vend_name,‘(‘,vend_country,‘)‘) from vendors;
Rtrim, 刪除右側空格
select a+b as c from table order by c;
2.資料處理函數:
文本處理函數:upper ex: select upper(a) as a_upper_case from table;
trim,rtrim,trim
left , ex: select left(‘abcdefg‘, 3), 返回abc
locate: 返回字串str第一次出現的子串SUBSTR的位置 ,ex: select locate(‘a‘,‘bac‘); 得到2
substring: http://www.yiibai.com/sql/sql_function_substring.html
時間:欄位為datetime類型,儲存為 日期+時分秒,通過date,time函數過濾欄位
ex: select xx from table date(xxx)=‘2015-01-01‘;
year,month,day過濾出年月日
資料處理: ex : select Pi(); select rand(); select abs(-1);
3.聚集合函式:avg , 忽略null的欄位)
count ,count(*) 不管是否為空白,還是null;count(column)對特定列中具有值進行計算
max,min,sum
4.group by , ...from table where xxx group by xxx order by xxxx
處聚聚計算語句外,select語句中的每個列都必須在group by 字句中給出
having xxx ,必須使用運算式,不可使用別名,ex: 如下 不可使用having ordertotal
ex, select order_num,sum(quantity*item_price) as ordertotal
from orderitems
group by order_num
having sum(quantity*item_price)>=50
order by ordertotal;
ps: 對 orderitems中的資料,根據order_num 分組,每組計算sum(quantity*item_price),
當 sum(quantity*item_price)>=50時,過濾出來,按照ordertotal升序顯示
select student_id,avg(score) as average_score from scores
group by student_id having avg(score)<60
order by average_score;
mysql(2)