GVKun编程网logo

MySQL 中的 FOUND_ROWS () 与 ROW_COUNT () 函数(mysql中row_number() over)

1

如果您想了解MySQL中的FOUND_ROWS()与ROW_COUNT()函数的相关知识,那么本文是一篇不可错过的文章,我们将对mysql中row_number()over进行全面详尽的解释,并且为您

如果您想了解MySQL 中的 FOUND_ROWS () 与 ROW_COUNT () 函数的相关知识,那么本文是一篇不可错过的文章,我们将对mysql中row_number() over进行全面详尽的解释,并且为您提供关于Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)、Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''、centos7 设置 mysql 自启动的配置文件中 [Service] User=mysql Group=mysql,user 和 group 这边的 mysql 是指的什么?centos 的登录用户名?、ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock...的有价值的信息。

本文目录一览:

MySQL 中的 FOUND_ROWS () 与 ROW_COUNT () 函数(mysql中row_number() over)

MySQL 中的 FOUND_ROWS () 与 ROW_COUNT () 函数(mysql中row_number() over)

原文地址:https://www.cnblogs.com/digdeep/p/4818660.html

 

移植 sql server 的存储过程到 mysql 中,遇到了 sql server 中的:

IF @@ROWCOUNT < 1

对应到 mysql 中可以使用 FOUND_ROWS () 函数来替换。

1. found_rows () 函数

文档地址:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_found-rows

1)found_rows () 的第一种使用情况 (带有 SQL_CALC_FOUND_ROWS,也带 limit):

SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT, but without running the statement again. To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
    -> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();

The second SELECT returns a number indicating how many rows the first SELECT would have returned had it been written without the LIMITclause.

前面的带有 limit 的 select 语句如果加上了 SQL_CALC_FOUND_ROWS,那么接下来执行的 SELECT FOUND_ROWS(); 将返回前面语句不带 limit 语句返回的行数

此种情况下,select found_rows () 和 select count (*) 有一个很小的区别:如果 userId 允许为 null,那么 select found_rows () 的结果可能要比 select count (*) 要小一些。因为前者等价于:select count (userId) from Users; 而该语句不会计算 userId 为 null 的行在内。而 count (*) 会计算在内。

2)found_rows () 的第二种 / 第三中使用情况 (带有 SQL_CALC_FOUND_ROWS):

In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement. If the statement includes a LIMIT clause, FOUND_ROWS() returns the number of rows up to the limit. For example, FOUND_ROWS() returns 10 or 60, respectively, if the statement includes LIMIT 10 or LIMIT 50, 10.

The row count available through FOUND_ROWS() is transient and not intended to be available past the statement following the SELECT SQL_CALC_FOUND_ROWS statement. If you need to refer to the value later, save it:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM ... ;
mysql> SET @rows = FOUND_ROWS();

If you are using SELECT SQL_CALC_FOUND_ROWS, MySQL must calculate how many rows are in the full result set. However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client.

1> 第二种使用情况 (带有 SQL_CALC_FOUND_ROWS,也没有带 limit ):

如果前面的 select 语句没有带 SQL_CALC_FOUND_ROWS,也没有带 limit ,那么后面的 SELECT FOUND_ROWS(); 返回的结果就是前面的select返回的行数;

2> 第三中使用情况 (带有 SQL_CALC_FOUND_ROWS,但是有带 limit ):

如果前面的 select 语句没有带 SQL_CALC_FOUND_ROWS,但是带有 limit,那么后面的 SELECT FOUND_ROWS(); 返回的结果就是limit语句到达的最大的行数,比如:select * from xxx limit 10; 到达的最大的行数为10,所以 found_rows() 返回10;比如 select * from xxx limit 50,10; 它要从第50行开始,再扫描10行,所以到达的最大的行数为60,所以found_rows() 返回60。

这里第一个 select found_rows () 返回 105,因为他是从偏移 100 的地方,再扫描 5 行,所以返回 105;但是第二个扫描的结果为空,select found_rows () 返回了 0!而不是 105,因为 where userId=999999 的结果为空,所以后面的 limit 100,5 根本就没有执行所以 select found_rows () 返回了 0

再看一个例子,更深入的理解其中情况下的 found_rows ():

上面 sql 中 user_Pwd=xx 的值都是一样的。可以看到这种情况下的 found_rows () 是对的 select 语句的中间结果,再 limit 时,此时的 limit 的扫描到的最大的行数。和原始表中的数据的行数,是没有关系的。他是对 select 的中间结果的 limit,然后才得到最后的结果集,再返回。

3)SQL_CALC_FOUND_ROWS and FOUND_ROWS() 适合使用的场景

SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. An example is a Web script that presents a paged display containing links to the pages that show other sections of a search result. Using FOUND_ROWS() enables you to determine how many other pages are needed for the rest of the result.

1> SQL_CALC_FOUND_ROW + limit + found_rows () 可以使用在分页的场合。

2> 不带 SQL_CALC_FOUND_ROW 的 found_rows () 可以使用在存储过程中判断前面的 select 是否为空

DELIMITER //
DROP PROCEDURE IF EXISTS loginandreg //

CREATE PROCEDURE loginandreg(
    OUT userId     BIGINT,
    IN user_Pwd                          VARCHAR(32),
    IN user_MobileCode                   VARCHAR(16),
    IN user_RegIP                        VARCHAR(16)
)
BEGIN
IF EXISTS(SELECT * FROM Users u WHERE u.user_MobileCode=user_MobileCode) THEN
    SELECT u.userId INTO userId FROM Users u WHERE u.user_MobileCode=user_MobileCode AND u.user_Pwd=user_Pwd;    
    IF FOUND_ROWS() < 1 THEN
        SELECT -1 INTO userId;
    END IF;
ELSE 
    INSERT INTO Users(user_Pwd,user_MobileCode,user_Visibility,user_Level,user_RegTime,user_RegIP,user_Collecter,user_Collected)
    VALUES (user_Pwd,user_MobileCode,6,6,NOW(),user_RegIP,0,0);
    SELECT LAST_INSERT_ID() INTO userId;
END IF;

END //
DELIMITER ;

上面存储过程中的:

SELECT u.userId INTO userId FROM Users u WHERE u.user_MobileCode=user_MobileCode AND u.user_Pwd=user_Pwd;    
    IF FOUND_ROWS() < 1 THEN
        SELECT -1 INTO userId;
    END IF;

就是一个很好的使用的例子。

这种存储过程的场景中就可以使用 mysql 的 FOUND_ROWS () 替换 sql server 存储过程中的 IF @@ROWCOUNT < 1 语句

--------------------------------------------------------------------------------------------------------------------------

2. row_count () 函数

文档地址:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_row-count

一句话,row_count () 函数一般用于返回被 update, insert, delete 实际修改的行数

In MySQL 5.6, ROW_COUNT() returns a value as follows:

  • DDL statements: 0. This applies to statements such as CREATE TABLE or DROP TABLE.

  • DML statements other than SELECT: The number of affected rows. This applies to statements such as UPDATEINSERT, or DELETE (as before), but now also to statements such as ALTER TABLE and LOAD DATA INFILE.

  • SELECT: -1 if the statement returns a result set, or the number of rows affected” if it does not. For example, for SELECT * FROM t1ROW_COUNT() returns -1. For SELECT * FROM t1 INTO OUTFILE ''file_name''ROW_COUNT() returns the number of rows written to the file.

  • SIGNAL statements: 0.

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows found”; that is, matched by the WHEREclause.

也就是说对于 update 语句,row_count () 默认返回的是实际被修改的行数;但是通过参数设置,也可以返回找到的行数 (或者说匹配的行数,受影响的行数),这样设置就能兼容于 Oracle  ps/sql 中 sql%rowcount 和 sql server 中的 @@RowCount

但是 row_count () 的结果和 mysql 的 JDBC driver 的默认行为却是不一致的,mysql jdbc 中的 Statement.getUpdateCount() 函数返回的是被找到的行数,而不是实际被修改的行数,如果要返回被实际修改的行,应该使用存储过程,相关链接说明:

http://stackoverflow.com/questions/17544782/how-to-tell-number-of-rows-changed-from-jdbc-execution

http://mybatis-user.963551.n3.nabble.com/Return-number-of-changed-rows-td3888464.html#a3903155

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html (这里包含了所有 mysql jdbc 链接可设置的参数)

useAffectedRows
Don''t set the CLIENT_FOUND_ROWS flag when connecting to the server (not JDBC-compliant, will break most applications that rely on "found" rows vs. "affected rows" for DML statements), but does cause "correct" update counts from "INSERT ... ON DUPLICATE KEY UPDATE" statements to be returned by the server.

Default: false
Since version: 5.1.7

该参数默认为 false,我们最好不要进行修改,如果修改了就和 JDBC 标准不兼容!如果需要返回实际被修改的行,应该使用存储过程 (使用 row_count ())

If you need to know how many records were *changed* (not that same as the number matching the where) then you might consider a stored
procedure.

但是设置 useAffectedRows=true; 有一个好处,就是它能正确的返回 insert ... on duplicate key update 的结果 (如果不设置为 true,返回的结果是错误的):参见:http://blog.sina.com.cn/s/blog_7325f5150101i3v4.html

mysql jdbc url 另外两个重要的参数 userUnicode 和 characterEncoding

useUnicode
Should the driver use Unicode character encodings when handling strings? Should only be used when the driver can''t determine the character set mapping, or you are trying to ''force'' the driver to use a character set that MySQL either doesn''t natively support (such as UTF-8), true/false, defaults to ''true'' Default: true Since version: 1.1g
characterEncoding
If ''useUnicode'' is set to true, what character encoding should the driver use when dealing with strings? (defaults is to ''autodetect'')
Since version: 1.1g

userUnicode 参数默认就是 true,而 characterEncoding 默认是自动侦测。所以一般

jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=UTF-8

可以简化为:jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8

关于对 mysql 复制的影响:

Important

FOUND_ROWS() is not replicated reliably using statement-based replication. This function is automatically replicated using row-based replication.
Important

ROW_COUNT() is not replicated reliably using statement-based replication. This function is automatically replicated using row-based replication.

注意:found_rows () 和 row_count () 在基于 语句的复制 环境中是不可靠的,它们自动使用 基于行的复制行为。

3. 总结

1)在存储过程迁移时, sql server 的 @@RowCount, oracle 的 sql%rowcount  可以被 mysql 的 found_rows () 和 row_count () 替代;

2)mysql 中的 found_rows () 的三种用法;

3)mysql jdbc url 另外两个重要的参数 userUnicode 和 characterEncoding;

     mysql jdbc 所有参数参见:http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)

Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)

ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)

 

 

 

原因:系统盘满了

[root@localhost opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 17G 0 100% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 477M 80M 372M 18% /boot
[root@localhost opt]#

解决:

删除大文件后,重启系统解决

 

 

[root@localhost mysql]# /opt/lampp/lampp status
Version: XAMPP for Linux 1.8.3-3
Apache is not running.
MySQL is not running.
ProFTPD is running.

 

df: 未处理文件系统
[root@localhost opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 17G 0 100% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 477M 80M 372M 18% /boot
[root@localhost opt]#

 

 

[root@localhost ~]# /opt/lampp/lampp status
Version: XAMPP for Linux 1.8.3-3
Apache is not running.
MySQL is running.
ProFTPD is running.

 

 

 

xampp 无法启动mysql 找不到mysql.sock

  (2016-02-24 23:21:24)
转载
  分类: 技术
出现的问题:
如果xampp中的mysql启动不了,出现ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)报错,
停止xampp的时候报:
-bash-4.1# /opt/lampp/lampp stop
Stopping XAMPP for Linux 1.8.2-6...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
fail.

解决办法:
如果网上一些方法不好用的话,可以试试以下方法:
1. 确定系统盘是否满了
#df -h
2. 删除/opt/lampp目录中的pid文件(删掉后xampp重启时会重建,如果不放心,可以先备份lampp目录)
删除mysql相关缓存:
#rm -rf /opt/lampp/var/mysql/VM_*  
删除proftp相关缓存:
#rm -rf /opt/lampp/var/proftpd.pid
如果找不到pid文件,可以搜一下:
#find /opt/lampp -name ''*.pid''

 

Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''

Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''

MySQL已经被我移到数据盘了,本地连接数据库会报错:Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''

但是远程是可以连接的,my.cnf设置mysql的根目录也改成了数据盘的地址,还要在加上client的参数,设置如下:

[client]
socket = /home/data/mysql/mysql.sock

之后重启下mysql就可以了

centos7 设置 mysql 自启动的配置文件中 [Service] User=mysql Group=mysql,user 和 group 这边的 mysql 是指的什么?centos 的登录用户名?

centos7 设置 mysql 自启动的配置文件中 [Service] User=mysql Group=mysql,user 和 group 这边的 mysql 是指的什么?centos 的登录用户名?

centos7 设置 mysql 自启动的配置文件中

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false

这里的

[Service]

User=mysql

Group=mysql,

user 和 group 这边的 mysql 是指的什么?centos 的登录用户名?还是其他呢?

ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock...

ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock...

今天在配置客户服务器的时候遇到了一个很奇怪的问题,MySQL能够正常启动和停止,但在使用mysql -u root -p进入mysql的时候,出现标题所示的错误

根据错误提示,/var/lib/mysql目录下面缺少mysql.sock文件,将数据库的data目录下的mysql.sock软链到/var/lib/mysql目录下面就好了,mysql.sock文件是数据库启动后自动生成的,没有启动时是没有的

今天关于MySQL 中的 FOUND_ROWS () 与 ROW_COUNT () 函数mysql中row_number() over的介绍到此结束,谢谢您的阅读,有关Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)、Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''、centos7 设置 mysql 自启动的配置文件中 [Service] User=mysql Group=mysql,user 和 group 这边的 mysql 是指的什么?centos 的登录用户名?、ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock...等更多相关知识的信息可以在本站进行查询。

本文标签: