PHP MySQL在行具有特定数据的位置与行具有特定数据的位置之间选择数据

时间:2013-06-12 07:37:20

标签: php mysql

这有点难以解释,但我希望从行中具有特定入口起点和终点的行范围之间的表中获取数据。

234 ****************************************************
235 some data
236 more data
237 even more data
238 etc...
239 ****************************************************

所以*之间是我感兴趣的数据。 问题是这会有所不同,因此行号每次都会有所不同。它可能在表格中出现不止一次,可以拉出每个条目。实际上是必要的。

如果您需要更多信息,请告诉我......

2 个答案:

答案 0 :(得分:1)

我认为你不能只在SQL中实现它,这就是我在PHP中使用两个查询和一些PHP逻辑来构建第二个查询的方法。

//this array is to simulate your table, don't make it.
$data = [1 => '*****', 'This', 'is', 'data', '*****', '*****', 'more', 'data', '*****', 'random', '*****', 'rows', '*****'];

//Run a query to get all the ids
$sql = "SELECT id FROM tbl WHERE data =  '*****' ORDER BY id ASC";
//assign them to an array - I did it manually.
$ids = [1, 5, 6, 9, 11, 13];

//Build the query to get the end sets.
$query = "SELECT * FROM tbl WHERE ";
while (count($ids)) {
    $start = array_shift($ids);
    $end = array_shift($ids);
    $query .= "(id >= $start AND id <= $end) OR ";
}
$query = trim($query, " OR ");
echo $query;

答案 1 :(得分:0)

您感兴趣的值存储在一个表格列(单元格)中?如果是这样,它是哪种数据类型?如果是文本数据,则可以使用MATCH或REGEXP函数http://dev.mysql.com/doc/refman/5.7/en/regexp.html#operator_regexp

但请注意,它会很慢。