php MySQL 选择优先级

标签 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;  

最佳答案

您可以拆分查询。

  $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;   

关于php MySQL 选择优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387453/

相关文章:

javascript - 选择表单后从数据库获取隐藏的输入值

mysql - 删除 phpmyadmin 中重复的前缀

mysql - 查询 : Find customers who have made purchase of every type of flower offered by shop

c# - 不为mysql存储过程c#获取参数

python - MySQL 'IF EXISTS'命令在python中使用时导致错误

php - 如何将特殊字符以UTF-8格式存储在数据库中

php - 限制 php 脚本可以从 cron 运行的实例数量

php - 不使用 gethostbyname 从 DNS 获取 IP?

php - 循环以垂直填充 html 表

php - 在一个查询中访问 3 个表