首页 > 编程知识 正文

mysql数据库部分常见函数,数据库中常用函数

时间:2023-12-27 22:28:09 阅读:327405 作者:WHJS

本文目录一览:

mysql中常用的聚合函数有哪些?

一、AVG

AVG(col):返回指定列的平均值

二、COUNT

COUNT(col):返回指定列中非NULL值的个数

三、MIN/MAX

MIN(col):返回指定列的最小值

MAX(col):返回指定列的最大值

四、SUM

SUM(col):返回指定列的所有值之和

五、GROUP_CONCAT

GROUP_CONCAT([DISTINCT] expr [,expr ...]

[ORDER BY {unsigned_integer | col_name | expr}

[ASC | DESC] [,col_name ...]]

[SEPARATOR str_val])

返回由属于一组的列值连接组合而成的结果

扩展资料

增加新用户

(注意:和上面不同,下面的因为是 MySQL 环境中的命令,所以后面都带一个分号作为命令结束符)

格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”

例1、增加一个用户 test1 密码为 abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以 root 用户连入 MySQL,然后键入以下命令:

grant select,insert,update,delete on *.* to test1@“%” Identified by “abc”;

但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见例2。

例2、增加一个用户 test2 密码为 abc,让他只可以在 localhost 上登录,并可以对数据库 mydb 进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。

grant select, insert, update, delete on mydb.* to test2@localhost identified by “abc”;

如果你不想 test2 有密码,可以再打一个命令将密码消掉。

grant select, insert, update, delete on mydb.* to test2@localhost identified by “”;

下面来看看 MySQL 中有关数据库方面的操作。注意:必须首先登录到 MySQL 中,以下操作都是在 MySQL 的提示符下进行的,而且每个命令以分号结束。

MySQL实现常用分析函数

分别在 MySQL5.7.25-log 和 8.0.16 环境中实现类似Oracle的分析函数(8.0版本中已支持,直接使用即可)。

一、创建测试数据

二、row_number() over()

三、rank() over()

四、dense_rank() over()

五、lag() over()

六、lead() over()

七、待补充

例1:不分组,全部数据添加序列号,类Oracle 的rownum伪列

例2:先按roomid分组,再按照deviceid,counter排序,类Oracle 的row_number() OVER(PARTITION BY ORDER BY )

例1:不分组,全部数据按 roomid 排序,再添加序号,类Oracle 的rank() OVER(ORDER BY)

例2:先按roomid分组,再按deviceid排序,类Oracle 的rank() OVER(PARTITION BY ORDER BY)

例1:不分组,全部数据按roomid排序,再添加序号,类Oracle 的dense_rank() OVER(ORDER BY)

例2:先按roomid分组,再按deviceid排序,类Oracle 的dense_rank() OVER(PARTITION BY ORDER BY)

例1:不分组,全部数据按roomid,deviceid升序排序,类Oracle 的lag() OVER(ORDER BY)

例2:先按roomid分组,再按roomid,deviceid排序,类Oracle 的lag() OVER(PARTITION BY ORDER BY)

例1:不分组,全部数据按roomid,deviceid,counter升序排序,类Oracle 的lead() OVER(ORDER BY)

例2:先按roomid分组,再按deviceid,counter排序,类Oracle 的lead() OVER(PARTITION BY ORDER BY)

mysql之聚合函数

mysql中有5种常用的聚合函数:sum()、avg()、max()、min()、count()

AVG():通过计算分组内指定字段值的和,以及分组内的记录数,算出分组内指定字段的平均值。

SUM():可以返回指定字段值的和。求和函数获取的是分组中的合计数据。如果涉及到多个字段分组,一定要知道字段之间有什么样的层次关系。

LEFT(str,n): 表示返回字符串str最左边的n个字符。

AVG()函数:通过计算分组内指定字段值的和,以及分组内的记录数,算出分组内指定字段的平均值。

MAX()函数:表示获取指定字段在分组中的最大值。

MIN()函数:表示获取指定字段在分组中的最小值。

COUNT()函数:了解数据集的大小。

mysql用户函数有哪些

用户函数?那应该是自己定义的函数吧

常用的函数有

1、MySQL 提供几个处理null的函数

1)、ifnull(expr1,expr2):如果expr1不为null,则返回expr1,否则返回expr2;

2)、nullif(expr1,expr2):expr1与expr2相等,则返回null,否则返回expr1;

3)、if(expr1,expr2,expr3):类似于三目运算符,expr1为true,返回expr2,否则返回expr3;

4)、isnull(expr1):判断expr1是否为null,expr1为null,返回true,否则返回false。

2、MySQL case函数

CASE 函数是一个流程控制函数,作用同c++中switch语句。有如下两种使用方法:

1)、用值比较

case value

when compareValue1 then result1

when compareValue2 then result2

……

else result

end

用value依次与各compareValue比较,相等则返回对应的result,并退出case函数。

2)、用条件判断

case

when condition1 then result1

when condition2 then result2

……

else result

end

各condition都为布尔表达式,从上到下判断,为true则返回对应的result,并退出case函数。

使用示例:

SELECT student_name,CASE

WHEN student_id3 THEN ‘初级班’

WHEN student_id=6 THEN '中级班'

ELSE ‘高级版'

END

FROM student_table;

说明:上面语句选择student_name与student_id两列,只是student_id用CASE 函数替换为对应的文字说明。

3、部分时间日期函数

1)、CURTIME():返回完整的时间类型值,如:’2016-08-10 22:13:30‘;

2)、CURDATE():返回当前日期,时间部分为0,如:’2013-08-10 00:00:00‘;

3)、ADDTIME(time1,time2):time1为time或datetime表达式,time2为time表达式,如:

addtime(’2016-08-10 22:20:00‘,’10:10:10')返回2016-08-11 08:30:10;

addtime(’2016-08-10 22:20:00‘,’1 10:10:10')返回2016-08-12 08:30:10;

addtime(’22:20:00‘,’10:10:10')返回32:30:10,所以只有时分秒时应注意相加后的值是否大于23:59:59!

注:上面的参数值都可带微秒[.xxxxxx]。

4、部分字符串函数

1)、LEFT(param,length):返回包含param左边的length 个字符的字符串。length 大于param 长度时返回整个param。

2)、RIGHT(param,length):返回包含param右边length 个字符的字符串。同理。

最大用处在于param 可以是选出的某个列的数据,如最基础的用法:

select LEFT(e.name,5) from table1 e;

三、常用分组和组函数

常用组函数:

注:distinct:区别的。指定计算是否包含所有重复值。

1、avg([distinct|all]expr):计算多行expr的平均值,其中expr可以是变量、常量或数据列,但其类型必须是数值型;

2、count(*|[distinct|all]expr):计算多行的记录数。expr同上,但数据类型可以是任意类型;

3、max(expr):计算多行expr的最大值,expr同count中;

4、min(expr);

5、sum([distinct|all]expr):计算多行expr的总和,expr可以是变量、常量或数据列,但数据类型必须是数值型。

group by 分组与having过滤:

语法:

#查询结果按分组列不重复的显示。

select * from tableName group by columnName1[,columnName2,……];

说明:对于MySQL 如果被分组的列对应的其它列有多个对应值,则只显示第一条记录的值。

pyMySQL库使用什么函数连接数据库

使用pymysql数据库访问MySQL数据库可分为以下步骤:

(1) 创建一个连接。创建一个连接对象,通过connect()方法连接到数据库。

(2) 获取光标。通过连接对象的cursor()方法创建一个cursor对象。

(3) 执行SQL语句。通过游标对象的Execute()、fetchone()或fetchall()方法执行SQL语句,实现基本的数据库操作,包括数据添加、更新、删除、查询等。

(4) 关闭光标。通过游标对象的Close()方法关闭游标。

(5) 关闭连接。通过connection对象的Close()方法关闭连接。Python视频教程+笔记+源代码+Q:498913868。

php mysqli 常用函数有哪些

php  中 mysqli 是个类,这个类的函数(方法)有:

mysqli::$affected_rows — Gets the number of affected rows in a previous MySQL operation

mysqli::autocommit — 打开或关闭本次数据库连接的自动命令提交事务模式

mysqli::begin_transaction — Starts a transaction

mysqli::change_user — Changes the user of the specified database connection

mysqli::character_set_name — 返回当前数据库连接的默认字符编码

mysqli::$client_info — Get MySQL client info

mysqli::$client_version — Returns the MySQL client version as a string

mysqli::close — 关闭先前打开的数据库连接

mysqli::commit — 提交一个事务

mysqli::$connect_errno — Returns the error code from last connect call

mysqli::$connect_error — Returns a string description of the last connect error

mysqli::__construct — Open a new connection to the MySQL server

mysqli::debug — Performs debugging operations

mysqli::dump_debug_info — 将调试信息输出到日志

mysqli::errno — 返回最近函数调用的错误代码

mysqli::$error_list — Returns a list of errors from the last command executed

mysqli::$error — Returns a string description of the last error

mysqli::$field_count — Returns the number of columns for the most recent query

mysqli::get_charset — Returns a character set object

mysqli::get_client_info — Get MySQL client info

mysqli_get_client_stats — Returns client per-process statistics

mysqli_get_client_version — 作为一个整数返回MySQL客户端的版本

mysqli::get_connection_stats — Returns statistics about the client connection

mysqli::$host_info — 返回一个表述使用的连接类型的字符串

mysqli::$protocol_version — 返回MySQL使用的协议版本号

mysqli::$server_info — 返回MySQL服务器的版本号

mysqli::$server_version — 作为一个整数返回MySQL服务器的版本

mysqli::get_warnings — Get result of SHOW WARNINGS

mysqli::$info — Retrieves information about the most recently executed query

mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()

mysqli::$insert_id — Returns the auto generated id used in the last query

mysqli::kill — Asks the server to kill a MySQL thread

mysqli::more_results — Check if there are any more query results from a multi query

mysqli::multi_query — Performs a query on the database

mysqli::next_result — Prepare next result from multi_query

mysqli::options — Set options

mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down

mysqli::poll — Poll connections

mysqli::prepare — Prepare an SQL statement for execution

mysqli::query — 对数据库执行一次查询

mysqli::real_connect — 建立一个 MySQL 服务器连接

mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection

mysqli::real_query — 执行一个mysql查询

mysqli::reap_async_query — Get result from async query

mysqli::refresh — Refreshes

mysqli::release_savepoint — Removes the named savepoint from the set of savepoints of the current transaction

mysqli::rollback — 回退当前事务

mysqli::rpl_query_type — Returns RPL query type

mysqli::savepoint — Set a named transaction savepoint

mysqli::select_db — 选择用于数据库查询的默认数据库

mysqli::send_query — 发送请求并返回结果

mysqli::set_charset — 设置默认字符编码

mysqli::set_local_infile_default — Unsets user defined handler for load local infile command

mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command

mysqli::$sqlstate — Returns the SQLSTATE error from previous MySQL operation

mysqli::ssl_set — Used for establishing secure connections using SSL

mysqli::stat — Gets the current system status

mysqli::stmt_init — 初始化一条语句并返回一个用于mysqli_stmt_prepare(调用)的对象

mysqli::store_result — Transfers a result set from the last query

mysqli::$thread_id — Returns the thread ID for the current connection

mysqli::thread_safe — 返回是否是线程安全的

mysqli::use_result — Initiate a result set retrieval

mysqli::$warning_count — Returns the number of warnings from the last query for the given link

以上函数清单直接来自  网站。你可以进入该网站参看。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。