函數中可以將字段名當作變量來用,變量的值就是該列對應的所有值;在整理98在線字典數據時(http://zidian.98zw.com/),有這要一個需求,想從多音字duoyinzi字段值提取第一個拼音作為拼音pinyin字段的值,如:duoyinzi(ā,á,ǎ,à,a),想提取ā作為pinyin的值;數據有好幾萬條,不想用程序一條條處理,只想用一個sql來實現,后來了解了下MYSQL常用內置函數,是可以做到的;sql:UPDATE ol_zidian set pinyin=LEFT(duoyinzi,LOCATE(duoyinzi,',')); 意思是,讓pinyin的值等於douyinzi左邊第一次出現,號的位置的值;
以下是常用的MYSQL常用內置函數詳解說明,希望大家用幫助
一、數值函數
abs(x):返回絕對值
ceil(x):返回大於x的最小整數
floor(x):返回小於x的最大整數
mod(x,y):返回x與y的模
rand():返回0-1之間的隨機數 select round(rand()*100);
round(x,y):返回參數x的y位小數的四舍五入結果
truncate(x,y):返回數字x截斷為y位小數的結果
二、字符串函數
PHP中用的,MYSQL中大部分都有
1.concat(s1,s2....sn):把傳入的參數連接成一個字符串
2.insert(str,x,y,insert):從str的x位置開始,替換y長度的字符串為insert
select insert('abcdefg',2,3,'hello');
3.lower(str),upper(str):將字符串轉換為大寫,小寫
4.left(str,x) right(str,x) 返回str左邊(右邊)x個字符,x為null則返回null
5.lpad(str,n,pad) rpad(str,n,pad) 用pad對字符串str從最左邊(右邊)進行填充,直到總長度
達到n
select name,lpad(name,10,'#'),rpad(name,10,'(') from cats;
6.trim(),ltrim(),rtrim()去掉兩邊,左邊,右邊空格
select concat('1',trim(' abc '),'2'),concat('1',ltrim(' abc '),'2'),concat
('1',rtrim(' abc '),'2')\G;
7.replace(str,a,b) 在字符串str中用字符串b替換所有的字符串a
8.strcmp(s1,s2):如果S1比S2小,返回-1;如果S1比S2大則返回1;如果相等則返回0(比較的是
ASC2碼)
9.substring(str,x,y) 返回字符串str中從位置x起,長度為y的子字符串
三、日期函數
用PHP時間戳來完成
curdate() curtime() now(); select curdate();
select unix_timestamp(now());
select from_unixtime(1331110656);
select week(now()),year(now());
select hour(curtime()),minute(curtime());
select monthname(now());
select date_format(now(),"%Y-%m-%d %H:%i:%s");
四、流程控制語句
create table salary(id int,salary decimal(9,2));
if(value,t,f); select id,salary,if(salary>300,'high','low') from salary;
ifnull(t,f) select id,salary,ifnull(salary,0) from salary;
case when [value1] then [result1]...else[default]end;
select case when salary<=300 then 'low' else 'high' end from salary;
五、其它函數(\s可以看到)
database() select database();
version()
user()
inet_aton(ip) 將字符串地址轉換為網絡地址
password() 對mysql用戶加密
md5() 對用戶密碼加密
select * from mysql.user \G;
MySQL中的全文索引類型FULLTEXT的索引。 FULLTEXT 索引僅可用於 MyISAM 表;他們可以從CHAR、VARCHAR或TEXT列中作為CREATE TABLE語句的一部分被創建,或是隨后使用ALTER TABLE 或 CREATE INDEX被添加。對於較大的數據集,將你的資料輸入一個沒有FULLTEXT索引的表中,然后創建索引,其速度比把資料輸入現有FULLTEXT索引的速度更為快。
