本文目錄一覽:
- 1、mysql中常用的聚合函數有哪些?
- 2、MySQL實現常用分析函數
- 3、mysql之聚合函數
- 4、mysql用戶函數有哪些
- 5、pyMySQL庫使用什麼函數連接資料庫
- 6、php mysqli 常用函數有哪些
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
以上函數清單直接來自 網站。你可以進入該網站參看。
原創文章,作者:IJUM,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140601.html