php MySQL选择优先级

时间:2016-09-08 09:35:51

标签: php mysql

$query = "SELECT * FROM posts WHERE language='$lang' AND (title LIKE '%$search%' OR author LIKE '%$search%' OR year LIKE '%$search%')";  

这正是应该做的。但我想做的是将“头衔”作为优先事项。但是现在看起来(每次搜索都在html的下拉列表中),它简单地显示它没有优先级。因此标题可以位于最底层,作者位于顶部。顺序错误。我想以某种方式总是把头衔放在首位。

如何?

$output = '';  
      $lang = $_SESSION["lang"];
      $search = $_POST["query"];
      $query = "SELECT * FROM posts WHERE language='$lang' AND (title LIKE '%$search%' OR author LIKE '%$search%' OR year LIKE '%$search%')";  
      $result = mysqli_query($connect, $query);  
      $output = '<ul class="list-unstyled">';  
      if(mysqli_num_rows($result) > 0)  
      {  
           while($row = mysqli_fetch_array($result))  
           { 

                $output .= '<a href="'.$url.'/'.$lang.'/'.$row["url"].'/"><li>'.$row["book"].'</li></a>';  
           }  
      }  
      else  
      {  
           $output .= 'Not found.';  
      }  
      $output .= '</ul>';  
      echo $output;  

3 个答案:

答案 0 :(得分:1)

您可以拆分查询。

  $output = '';  
  $lang = $_SESSION["lang"];
  $search = $_POST["query"];



      $query2 = "SELECT * FROM posts WHERE language='$lang' AND title LIKE '%$search%'";
      $result2 = mysqli_query($connect, $query2);  
      $output = '<ul class="list-unstyled">';  
      if(mysqli_num_rows($result2) > 0)  
      {  
           while($row = mysqli_fetch_array($result2))  
           { 

                $output .= '<a href="'.$url.'/'.$lang.'/'.$row["url"].'/"><li>'.$row["book"].'</li></a>';  
           }  
      }  
      else  
      {  
           $output .= 'Not found.';  
      }  

      $query = "SELECT * FROM posts WHERE language='$lang' AND (author LIKE '%$search%' OR year LIKE '%$search%')";  
      $result = mysqli_query($connect, $query);  


      if(mysqli_num_rows($result) > 0)  
      {  
       while($row = mysqli_fetch_array($result))  
       { 

            $output .= '<a href="'.$url.'/'.$lang.'/'.$row["url"].'/"><li>'.$row["book"].'</li></a>';  
       }  
     }  
      else  
     {  
       $output .= 'Not found.';  
     }  


  $output .= '</ul>'; 
  echo $output;   

答案 1 :(得分:0)

ORDER BY应该在这里为你解决问题:

http://www.w3schools.com/sql/sql_orderby.asp

$query = "
SELECT book
     , title
     , url
  FROM posts 
 WHERE language='$lang' 
   AND ( 
         title LIKE '%$search%' 
         OR 
         author LIKE '%$search%' 
         OR 
         year LIKE '%$search%'
        )
  ORDER BY title ASC
       , author ASC
       , book ASC
"; 

我已经通过“作者”添加了一个可选订单。和&#39;预订&#39; (订购的优先级从&#39; title&#39;开始,然后&#39;作者&#39;最后&#39; book&#39;) - 您可以将其更改为您需要的任何内容{} 1}}(升序)或ASC(降序)顺序。

我还建议您考虑使用bind params而不是直接将变量传入SQL以防止SQL注入。

Mysqli Bind Param文档

http://php.net/manual/en/mysqli-stmt.bind-param.php

非常好SO在这里发布了有关SQL注入的帮助和更多信息

How can I prevent SQL injection in PHP?

另外 - 尽可能避免使用DESC,并且仅SELECT * FROM...输出您需要的信息。您也可以通过这种方式SELECT更好(意味着可以更快地从数据库中检索数据)。

答案 2 :(得分:0)

您可以使用评分系统为每个匹配分数,然后按匹配分数排序。因此,对于标题的匹配获得更高的分数,并且对于作者的匹配获得次高,等等。我将在这里重写查询:

SELECT *,
(
    CASE
        WHEN title LIKE '%$search%'  THEN 100
        WHEN author LIKE '%$search%' THEN 10
        WHEN year LIKE '%$search%'   THEN 1
    END
) AS matchScore
FROM posts
WHERE
language='$lang' AND
(title LIKE '%$search%' OR author LIKE '%$search%' OR year LIKE '%$search%')
ORDER BY matchScore DESC