mysql:什么是SELECT的ROW_COUNT()信息函数?

c8ib6hqw  于 2023-02-28  发布在  Mysql
关注(0)|答案(4)|浏览(198)

我正在寻找一个内置的MySQL信息函数,它返回刚刚选择的行数,就像ROW_COUNT()对INSERT、UPDATE等函数所做的那样。
在存储过程中,我希望这样做:

...
select episode_name from bestLOSTepisodes
set _episodes_found = SELECTED_ROWS();
...

我做了大量的挖掘工作,但没有找到像SELECTED_ROWS()这样的函数。

u0sqgete

u0sqgete1#

您可能需要使用FOUND_ROWS()函数。
示例:

CREATE TABLE users (id int, name varchar(20), age int);

INSERT INTO users VALUES (1, 'Bob', 19);
INSERT INTO users VALUES (2, 'Jack', 25);
INSERT INTO users VALUES (3, 'Paul', 15);
INSERT INTO users VALUES (4, 'Steve', 35);
INSERT INTO users VALUES (5, 'Mark', 17);

SELECT * FROM users WHERE age > 18;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | Bob   |   19 |
|    2 | Jack  |   25 |
|    4 | Steve |   35 |
+------+-------+------+
3 rows in set (0.00 sec)

SELECT FOUND_ROWS() as num_of_rows_found;
+-------------------+
| num_of_rows_found |
+-------------------+
|                 3 |
+-------------------+
1 row in set (0.00 sec)
mkshixfv

mkshixfv2#

您必须在查询中使用FOUND_ROWS和SQL_CALC_FOUND_ROWS:http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

llmtgqce

llmtgqce3#

FOUND_ROWS和SQL_CALC_FOUND_ROWS已经过时,不会在MySQL 8.0.17以上的版本中使用。
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

hiz5n14c

hiz5n14c4#

您可以尝试FOUND_ROWS()函数。

CREATE TABLE Emp(EmpID int,EMPNAME varchar(20),SALARY int);
INSERT INTO Emp VALUES (1,'Astin',6500);
INSERT INTO Emp VALUES (2,'Blade',4500);
INSERT INTO Emp VALUES (3,'Cabal',6500);
INSERT INTO Emp VALUES (4,'Brant',5200);
INSERT INTO Emp VALUES (5,'Sunny',7000);
SELECT * FROM EMP WHERE SALARY=6500; 
SELECT FOUND_ROWS() as SelectedRowCount;

相关问题