我遇到了MySQL LIKE未返回所有可能結果的問題。我有下面的代碼:查詢沒有返回所有可能的結果
<?php
$query = "SELECT `user_id`,
`rank`,
`habbo_name`,
`rating`,
`branch`
FROM `personnel`
WHERE status='active' AND
rating='(DIROPS)'
ORDER BY `habbo_name`";
$result = $con->prepare($query);
$result->execute();
while ($row = $result->fetch())
{
echo "<b>DIROPS:</b> " . htmlspecialchars($row['habbo_name']) . "<br>";
}
$query = "SELECT `user_id`,
`rank`,
`habbo_name`,
`rating`,
`branch`
FROM `personnel`
WHERE status='active' AND
rating='(CO TECOM)' AND
`branch`='usmc'
ORDER BY `habbo_name`";
$result = $con->prepare($query);
$result->execute();
while ($row = $result->fetch())
{
echo "<b>CO TECOM:</b> " . htmlspecialchars($row['habbo_name']) . "<br>";
}
$query = "SELECT `user_id`,
`rank`,
`habbo_name`,
`rating`,
`branch`
FROM `personnel`
WHERE status='active' AND
rating='(XO TECOM)' AND
`branch`='usmc'
ORDER BY `habbo_name`";
$result = $con->prepare($query);
$result->execute();
while ($row = $result->fetch())
{
echo "<b>XO TECOM:</b> " . htmlspecialchars($row['habbo_name']) . "<br>";
}
?>
<br>
<?php
$query = "SELECT `user_id`,
`rank`,
`habbo_name`,
`rating`,
`branch`
FROM `personnel`
WHERE status='active' AND
`rating` LIKE '%(TECOM%' OR
`rating` LIKE '%/TECOM%' AND
status='active'
ORDER BY `habbo_name`";
$result = $con->prepare($query);
$result->execute();
while ($row = $result->fetch())
{
echo "<b>TECOM:</b> " . htmlspecialchars($row['habbo_name']) . "<br>";
}
?>
這一點,因爲據我所知,應該打印出來誰擁有等級設置爲(DIROPS)的人,(CO TECOM),(XO TECOM),然後(TECOM或/ TECOM)。
我的問題是隻有DIROPS和其他幾個正在返回。這是結果我從代碼獲得:
任何人都可以讓我知道我錯了?謝謝!
編輯:記錄應當顯示: -
TEO-emo12 (DIROPS) RockerManiac123 (CO TECOM) Rach-L (Lugn) (XO TECOM) Reyess (TECOM) zachary1142 (AuXHiDef-) (TECOM) norsk.no (TECOM) lordoftjefly (mingrana) (PPO/TECOM) spen1000 (TECOM/DI)
編輯:添加記錄它應該顯示 – user2547576
你需要這麼多的查詢嗎?不會有人這樣做嗎? – Strawberry
性能不是問題,我是一個新手,所以我更容易將它分解爲單獨的查詢。更不用說他們在「評級」列中搜索不同的東西,然後用不同的前綴回顯出來。 – user2547576