我有两个查询应该返回我的DB中相对于当前行的下一行和上一行。
然而,它们都返回相同的东西,下一行,并且由于某种原因,第二个查询也返回一个空数据集。
代码:
<?php
// Credentials
$dbhost = "localhost";
$dbname = "buildingcodes";
$dbuser = "***";
$dbpass = "***";
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");
// Check Connection
if ($tutorial_db->connect_errno)
{
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}
$id = 'R401.4 Soil tests.'; //$_GET['id'];
echo 'id: ' . $id;
echo '</br>';
//////
$query = "SELECT * FROM codes WHERE subsection > '". $id ."' ORDER BY subsection LIMIT 1";
$result = $tutorial_db->query($query);
while($results = $result->fetch_array())
{
$result_array[] = $results;
}
echo '</br></br>';
if (isset($result_array))
{
foreach ($result_array as $result)
{
echo 'first q: ' . $result['subsection'];
echo '</br>';
}
}
/////
$query = "SELECT * FROM codes WHERE subsection < '". $id ."' ORDER BY subsection LIMIT 1";
$result = $tutorial_db->query($query);
while($results = $result->fetch_array())
{
$result_array[] = $results;
}
echo '</br>';
if (isset($result_array))
{
foreach ($result_array as $result)
{
echo 'second q: ' . $result['subsection'];
echo '</br>';
}
}
?>
输出:
id: R401.4 Soil tests.
first q: R401.4.1 Geotechnical evaluation.
second q: R401.4.1 Geotechnical evaluation.
second q:
但是应该输出:
id: R401.4 Soil tests.
first q: R401.4.1 Geotechnical evaluation.
second q: R401.3 Drainage.
从这组数据中
为什么这不会返回前一行?
答案 0 :(得分:0)
SELECT * from ss where subsection > 'R401.4 Soil tests'
ORDER BY SUBSTRING_INDEX(subsection, ' ', 1) ASC;
+-------------------------+
| subsection |
+-------------------------+
| R401.4.1 Geotech. |
| R401.4.2 Compressible |
| R402.1 Wood foundations |
+-------------------------+
3 rows in set (0.00 sec)
SELECT * from ss where subsection < 'R401.4 Soil tests'
ORDER BY SUBSTRING_INDEX(subsection, ' ', 1) DESC;
+---------------------+
| subsection |
+---------------------+
| R401.3 Drainage |
| R401.2 Requirements |
| R401.1 Application |
+---------------------+
3 rows in set (0.01 sec)
当subsection
之间dot(.)
之间的查询超过两位时,查询将无效。然后可能以下查询适用于NEXT子部分。
ORDER BY LENGTH(SUBSTRING_INDEX(subsection, ' ', 1)),
SUBSTRING_INDEX(subsection, ' ', 1) ASC;
但不确定是否为PREV小组工作。