简体   繁体   English

查询未返回所有可能的结果

[英]Query isn't returning all of the possible results

I'm having a problem where MySQL LIKE isn't returning all of the possible results. 我遇到一个问题,其中MySQL LIKE无法返回所有可能的结果。 I have the code below: 我有下面的代码:

<?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>&nbsp" . 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>&nbsp" . 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>&nbsp" . 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>&nbsp" . htmlspecialchars($row['habbo_name']) . "<br>";
}
?>

This, as far as I know, should print out the people who have rating set as (DIROPS), (CO TECOM), (XO TECOM) and then (TECOM or /TECOM). 据我所知,应该打印出评级为(DIROPS),(CO TECOM),(XO TECOM)然后是(TECOM或/ TECOM)的人员。

The problem I have is that only DIROPS and a couple of the others are being returned. 我的问题是只有DIROPS和其他几个被退回了。 This is the result I get from that code: 这是我从该代码得到的结果:

在此处输入图片说明

Could anyone let me know where I am going wrong? 谁能告诉我我要去哪里错了? Thanks! 谢谢!

EDIT: Records it should be showing :- 编辑:记录应该显示:-

 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) 

you should use one query instead of running same query multiple times for each condition , wrap all your rating conditions in a group with a parent AND operator and just loop through the result set ignore the multiple loops it will effect the execution time 您应该使用一个查询,而不是针对每个条件多次运行相同的查询,将所有rating条件与父AND运算符包装在一个组中,然后循环遍历结果集,而忽略多个循环将影响执行时间

SELECT `user_id`, 
                  `rank`, 
                  `habbo_name`, 
                  `rating`, 
                  `branch` 
             FROM `personnel` 
            WHERE status='active' AND (
                  `rating` LIKE '%(DIROPS)%' OR 
                  `rating` LIKE '%(CO TECOM)%' OR                      
                  `rating` LIKE '%(XO TECOM)%'   OR
                  `rating` LIKE '%(TECOM%'  OR 
                  `rating` LIKE '%/TECOM%' 
                 ) AND  `branch`='usmc' 
         ORDER BY `habbo_name`

OR 要么

 SELECT `user_id`, 
                  `rank`, 
                  `habbo_name`, 
                  `rating`, 
                  `branch` 
             FROM `personnel` 
            WHERE status='active' AND (
                  `rating` LIKE '%(DIROPS)%' OR 
                  `rating` LIKE '%(TECOM%'  OR 
                  `rating` LIKE '%/TECOM%' 
                 ) OR `rating`   IN
 (SELECT `rating` FROM `personnel` WHERE `branch`='usmc' 
  AND ( `rating` ='(CO TECOM)' OR `rating` ='(XO TECOM)'))   
         ORDER BY `habbo_name`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM