mysql 函式使用

2021-10-22 11:25:57 字數 1174 閱讀 6810

over不能單獨使用,要和分析函式:rank(),dense_rank(),row_number()等一起使用。

使用:

引數描述condition

必須,判斷條件

value_if_true

可選,當條件為true值返回的值

value_if_false

可選,當條件為true值返回的值

使用例項

編寫乙個 sql 查詢,查詢所有至少連續出現三次的數字。

+----+-----+

| id | num |

+----+-----+

| 1 | 1 |

| 2 | 1 |

| 3 | 1 |

| 4 | 2 |

| 5 | 1 |

| 6 | 2 |

| 7 | 2 |

+----+-----+

例如,給定上面的 logs 表, 1 是唯一連續出現至少三次的數字。

+-----------------+

| consecutivenums |

+-----------------+

| 1 |

+-----------------+

正解答案之一:

select 

distinct num as consecutivenums

from

(select num,

if(@pre=num,@count := @count+1,@count := 1) as nums,

@pre:=num

from logs as l ,

(select @pre:= null,@count:=1) as pc

) as n

where nums >=3;

目前只羅列部分函式的使用,後續慢慢積累總結?

sql中group by使用:

mysql函式使用

1 取到查詢條件為乙個欄位的擷取部分 select a.order no from t order a where a.deleted 0 and substring a.order no,1,8 order by a.add time desc limit 12 字段逗號隔開的函式使用find i...

mysql 常用函式使用

and year t2.checkout time year 函式接收date型別,返回乙個年份。還有month 函式,乙個用法 mysql select year 2018 01 01 year 2018 01 01 2018 1 row in set大於。小於的,需要轉義。and t2.age ...

mysql函式使用集合

mysql函式使用集合整理 後續有用到的函式,會新增進來 strlist格式 引數以 分隔 如 1,2,6,8 這個函式很適合用來查詢包含多個值得列 find in set str,strlist select from student where find in set 值,欄位名 返回傳入的天數...