mysql自帶 Mysql自帶函式

2021-10-18 20:49:25 字數 1259 閱讀 8724

1. ifnull()函式

格式:ifnull(expr1,expr2)

說明:如果expr1不是null,ifnull()返回expr1,否則它返回expr2。

ifnull()返回乙個數字或字串值,取決於它被使用的上下文環境。

舉例:mysql> select ifnull(1,0);

| ifnull(1,0) |

| 1 |

1 row in set

mysql> select ifnull(1/0,'nowamagic');

| ifnull(1/0,'nowamagic') |

| nowamagic |

1 row in set

2. if()函式

格式:if(condition,a,b)

說明:當condition為true時,返回a;當condition為false時,返回b。

if()的返回值為數字值或字串值,具體情況視其所在語境而定。

舉例:mysql> select if(1>2,2,3);

-> 3

mysql> select if(1<2,'yes ','no');

-> 'yes'

3. group_concat()

格式:group_concat([distinct] 要合併的字段 [order by asc/desc 排序字段] [separator '分隔符'])

說明:用來應付如一對多情況的變體情況。 該方法往往配合group by 一起使用。

舉例:mysql>select locus,id,journal from info where locus in('ab086827','af040764');

| locus | id | journal |

| ab086827 | 1 | unpublished |

| ab086827 | 2 | submitted (20-jun-2002) |

| af040764 | 23 | unpublished |

| af040764 | 24 | submitted (31-dec-1997) |

mysql>select locus,group_concat(id) from info where locus in('ab086827','af040764') group by locus;

| locus | group_concat(id) |

| ab086827 | 1,2 |

| af040764 | 23,24 |

Mysql自帶函式

1.ifnull 函式 格式 ifnull expr1,expr2 說明 如果expr1不是null,ifnull 返回expr1,否則它返回expr2。ifnull 返回乙個數字或字串值,取決於它被使用的上下文環境。舉例 mysql select ifnull 1,0 ifnull 1,0 1 1...

linux下解除安裝自帶mysql

1 確認系統安裝了哪些rpm包 root host1 etc rpm qa grep i mysql mysql server 5.1.71 1.el6.x86 64 mysql 5.1.71 1.el6.x86 64 perl dbd mysql 4.013 3.el6.x86 64 mysql ...

MySQL常用的自帶函式

mysql自帶函式十分豐富,合理使用可以減少很多編碼工作。數學函式主要用於處理數字,包括整型 浮點數等。數學函式包括絕對值函式 正弦函式 余弦函式 獲取隨機數的函式等。abs x 返回x的絕對值 mod n,m 或 返回n被m除的餘數 floor x 返回不大於x的最大整數值 ascii str 返...