php - 使用 php 在一些 mysql 行中查找模式

标签 php mysql arrays design-patterns matching

我在 mysql 中有一个包含 2 列的表:id(auto_increment) 和 value(int 0/1)。我想用 PHP 查找表中有多少匹配模式“101”。示例:

id | value
----------
1  |   1
2  |   1   --
3  |   0    |  => (1)
4  |   1   --
5  |   0     | => (2)
6  |   1   -- 
7  |   0
8  |   0
9  |   1
10 |   1
.. |   ..
.. |   ..
5000 |  1  --
5001 |  0    |  => (n)
5002 |  1  --
5003 |  1
...  |  ...

使用数组比通过mysql查找模式(将mysql的值导入数组)更快?

有人知道如何使用 PHP 查找整行中“101”的出现次数吗?

谢谢 - jack -

最佳答案

以下是您可以在 SQL 中执行此操作的方法。

select count(*) match_count
from TheTable t1
join TheTable t2 on t1.id+1 = t2.id
join TheTable t3 on t1.id+2 = t3.id
where t1.value = 1 and t2.value = 0 and t3.value = 1

关于php - 使用 php 在一些 mysql 行中查找模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679426/

相关文章:

php - 行检索错误

javascript - 将数组的随机正值转换为负值

Java 2D数组从A点到B点有障碍物的最短路径

php - 使用 mysqli 多重查询显示搜索结果

php - MySQL 搜索查询类似

php - 注销时从 ci_sessions 表中删除 session

MySQL 表达式检索百分比

mysql - 我的 Mysql 触发器有什么问题 - 抛出语法错误

python - numpy - 在点网格上评估函数

php - 通知栏 : Am I going the right way? 的 Node.js 和 socket.io