PHP MySQL 在具有特定数据的行和具有特定数据的行之间选择数据

标签 php mysql

这有点难以解释,但我想从一系列行之间的表中获取数据,其中该行具有特定的入口起点和终点。

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/

相关文章:

MySQL SELECT REPLACE 字符串的一部分

php - 如何在 PHP 中显示在线用户?

php - IIS + PHP - 无法使用旧身份验证连接到 MySQL 4.1+

php - 如何对以下 PHP mysql_* 代码执行 SQL 注入(inject)?

mysql - 从查询 MySQL 中获取 Concat 函数中的本地化月份名称

python - 用wamp的mysql安装python-mysql

php - 在php/mysql中插入数组数据

php - mysqli_num_rows始终为1

mysql - SQLite 和 VARCHAR - 换行符

php - SQL ORDER BY 具有多个参数的情况