这有点难以解释,但我想从一系列行之间的表中获取数据,其中该行具有特定的入口起点和终点。
234 ****************************************************
235 some data
236 more data
237 even more data
238 etc...
239 ****************************************************
所以 * 之间是我感兴趣的数据。 问题是这会有所不同,因此行号每次都会不同。并且它可能会在表中出现多次,因此可以提取每个条目。其实是有必要的。
如果您需要更多信息,请告诉我...
最佳答案
我认为您不能仅使用 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;
关于PHP MySQL 在具有特定数据的行和具有特定数据的行之间选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060069/