Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
Php MySQL/MySQLi从表中选择,直到找到给定值为止_Php_Mysql_Mysqli_Select Until - Fatal编程技术网

Php MySQL/MySQLi从表中选择,直到找到给定值为止

Php MySQL/MySQLi从表中选择,直到找到给定值为止,php,mysql,mysqli,select-until,Php,Mysql,Mysqli,Select Until,我在这方面搜索了很长时间,似乎找不到适合我的东西,因为每个类似的问题都是关于达到某个列的某个和 我正在寻找一种方法来搜索表中所有具有多个公共列值的记录——它们不是表中的连续记录,但在考虑公共值时它们必须是连续记录——并检索所有记录,直到一个特定列具有不同的值 鉴于数据: PrimaryID SecondaryID TertiaryID Limit DateTime 12 9 4 1 201

我在这方面搜索了很长时间,似乎找不到适合我的东西,因为每个类似的问题都是关于达到某个列的某个和

我正在寻找一种方法来搜索表中所有具有多个公共列值的记录——它们不是表中的连续记录,但在考虑公共值时它们必须是连续记录——并检索所有记录,直到一个特定列具有不同的值

鉴于数据:

PrimaryID   SecondaryID   TertiaryID   Limit       DateTime
   12            9             4           1       2013-03-22 00:00:14
   11            9             4           1       2013-03-22 00:00:13
   10           54            11           0       2013-03-22 00:00:12
    9            9             4           1       2013-03-22 00:00:11
    8            9            23           1       2013-03-22 00:00:10
    7            9             4           1       2013-03-22 00:00:09
    6            9             4           0       2013-03-22 00:00:08
    5            9            16           0       2013-03-22 00:00:07
    4           72            32           0       2013-03-22 00:00:06
    3            9             4           1       2013-03-22 00:00:05
    2            9             4           1       2013-03-22 00:00:04
    1            9             4           0       2013-03-22 00:00:03
我正在尝试检索SecondaryID=9和TertiaryID=4的记录,直到Limit=0,并按DateTime DESC排序

使用PrimaryID 12作为起点,我希望:

PrimaryID   SecondaryID   TertiaryID   Limit       DateTime
   12            9             4           1       2013-03-22 00:00:14
   11            9             4           1       2013-03-22 00:00:13
    9            9             4           1       2013-03-22 00:00:11
    7            9             4           1       2013-03-22 00:00:09
以PrimaryID 3为起点:

PrimaryID   SecondaryID   TertiaryID   Limit       DateTime
    3            9             4           1       2013-03-22 00:00:05
    2            9             4           1       2013-03-22 00:00:04
目前,我正在检索SecondaryID=9和TertiaryID=4的每个结果,然后在PHP中循环——目前有数百个结果,而且每周都在增长,因此在生产环境中运行速度太慢

我也很乐意接受一个特定于MySQLi的解决方案,因为我们即将向MySQLi而不是MySQL过渡。

查询将是

SELECT * FROM table_name 
WHERE PrimaryID <= 12(your_id) AND SecondaryID=9 and TertiaryID=4 AND Limit=1 
ORDER BY DateTime DESC

它已经过测试并且可以工作了

我意识到我一直在用错误的方式进行这个查询

我不需要从数据中的一个给定点开始倒数,而是需要查找上一次出现的Limit=0,然后向前查询,直到我的初始PrimaryID

结果查询覆盖了我需要的所有内容

SELECT * FROM table1 WHERE SecondaryID='9' AND TertiaryID='4' AND `Limit`='1' AND PrimaryID<='3' AND `DateTime`>= (SELECT `DateTime` FROM table1 WHERE SecondaryID='9' AND TertiaryID='4' AND `Limit`='0' AND PrimaryID<='3' ORDER BY `DateTime` DESC LIMIT 1) ORDER BY `DateTime` DESC

抱歉,但这不会给出我需要的结果集。对于PrimaryID=12,它还将返回PrimaryID为3和2的行,这不应该在我需要的结果集中,因为PrimaryID=6的行的Limit=0。我需要查询从一个记录中检索所有与SecondaryID和TertiaryID匹配的记录,并在时间上后退,直到Limit=0的记录存在。从本质上说,我使用查询来确定Limit字段从0更改为1的时间,以确定当前活动状态何时开始。