Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
遍历MySQL行_Mysql - Fatal编程技术网

遍历MySQL行

遍历MySQL行,mysql,Mysql,我有一个简单的MySQL表,由单词和相关数字组成。每个单词的数字都是唯一的。我想找到索引大于给定数字的第一个单词。例如: ----------------------- | WORD: | F_INDEX: | |---------------------| | a | 5 | | cat | 12 | | bat | 4002 | ----------------------- 如果给我数字“9”,我希望返回“cat”

我有一个简单的MySQL表,由单词和相关数字组成。每个单词的数字都是唯一的。我想找到索引大于给定数字的第一个单词。例如:

-----------------------
| WORD:    | F_INDEX: |
|---------------------|
| a        | 5        |
| cat      | 12       |
| bat      | 4002     |
-----------------------
如果给我数字“9”,我希望返回“cat”,因为它是索引大于9的第一个单词

我知道我可以通过查询得到已排序行的完整列表:

SELECT * FROM table_name ORDER BY f_index;
但是,我想做一个MySQL查询来实现这一点。(困惑在于我不确定如何在查询中跟踪当前行)。我知道你可以这样循环:

CREATE PROCEDURE looper(desired_index INT)
BEGIN 
    DECLARE current_index int DEFAULT 0
    // Loop here, setting current_index to whatever the next rows index is, 
    // then do a comparison to check it to our desired_index, breaking out 
    // if it is greater.
END;
任何帮助都将不胜感激。

试试以下方法:

SELECT t.word
     , t.f_index
  FROM table_name t
 WHERE t.f_index > 9 
 ORDER 
    BY t.f_index
 LIMIT 1
让数据库返回所需的行要比提取一大堆行并找出所需的行有效得多

为了获得此查询的最佳性能,您需要在表名(f\u index,word)上设置一个索引

试试这个:

SELECT t.word
     , t.f_index
  FROM table_name t
 WHERE t.f_index > 9 
 ORDER 
    BY t.f_index
 LIMIT 1
让数据库返回所需的行要比提取一大堆行并找出所需的行有效得多


为了获得此查询的最佳性能,您需要在表名(f\u index,word)
上设置一个索引

为什么不使用MYSQL语句从f_索引中检索到的第一个项,其中f_索引大于传入的值

例如:

select word from table_name
where f_index > desired_index
order by f_index
limit 1

为什么不使用MYSQL语句来检索从f_索引中找到的第一个项,其中f_索引大于传入的值

例如:

select word from table_name
where f_index > desired_index
order by f_index
limit 1

我我不知道这是件事。完美的谢谢,我。。我不知道这是件事。完美的非常感谢。