开发者

Order a mysql query alphabetically

Let's say that I have the following code:

SELECT * FROM table where company LIKE '%Auto%'

And I receive more resul开发者_JS百科ts, and I want to have an option to sort the results alphabetically, let's say that the user wants to sort the search results for the ones which start with "C"!

Best Regards,


Well, it seems that you are talking about two different things. If you are interested in sorting you would need to use the ORDER BY clause:

SELECT * FROM table ORDER BY name

If you want to filter the results by items that start with the letter 'C' then you would want to add another LIKE clause with that letter:

SELECT * FROM table where company LIKE '%Auto%' AND name LIKE 'C%'

Additionally you'll notice that the name filter only has the % after the query. This is the syntax for "starts with"


Use the ORDER BY clause:

SELECT *
FROM table
where company LIKE '%Auto%'
order by company


add ORDER BY company, assuming you want to sort by the company value.


try it..... its my working example sorting the record alphetical order A B C D ....Z when i click A Letter then its show all name start with A letter ,and click C Letter then its show all name start with C letter

     <?php
 $host = "localhost";  
 $user = "root";
 $pw = "";
 $database = "test";

 $con = mysql_connect($host,$user,$pw)
   or die("Cannot connect to mySQL.");

 mysql_select_db($database,$con)
   or die("Cannot connect to database.");




$errormsg= "No Record Found...!";


$alpha="%";
if (isset($_REQUEST['alpha'])) {
$alpha = $_REQUEST['alpha']."%";
}
$q1 = mysql_query("select * from registration where firstname like '$alpha%' " );





?>

<div >
       <form action="" method="post" >
<table  >
                         <td><div  style="float:left;">
  <CENTER>
     <FONT COLOR=Green>Sort by Alphabet:</FONT>  
                                <A HREF="<?php echo $_SERVER['PHP_SELF']; ?>">All</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=A">A</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=B">B</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=C">C</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=D">D</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=E">E</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=F">F</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=G">G</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=H">H</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=I">I</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=J">J</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=K">K</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=L">L</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=M">M</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=N">N</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=O">O</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=P">P</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Q">Q</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=R">R</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=S">S</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=T">T</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=U">U</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=V">V</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=W">W</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=X">X</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Y">Y</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Z">Z</A>
   </CENTER>

         </div>   


   </td></tr>

           <tbody>
    <?php



    $i=1;
    $num_rows=mysql_num_rows($q1);
    if($num_rows==0)
    {
    ?>
   <div align="center">
    <div class="alert alert-danger alert-dismissable">

      <?php echo $errormsg; ?></div>
    </div>
           <?php
    }
    else
    {
    while($roww=mysql_fetch_array($q1))
    {                
      ?>
           <tr>
    <td><div style="height:100px;float:left;">
        <input name="" type="checkbox" value="">
      </div>
       <div><a href="#">  <b><?php echo $roww["firstname"]; ?></b></a><br />
       :&nbsp;<?php echo $roww["firstname"]; ?><br />
        <?php echo $roww["lastname"]; ?><br />

        <?php echo $roww["gender"]; ?><br />
      </div></td>
   </tr>
           <?php }
          $i++;
          }

          ?>
           </tbody>

         </table>
       </form>
     </div>
   </body></html>
  <SCRIPT LANGUAGE=JavaScript>
       <!--
         document.write(ALPHABET())
       //-->
     </SCRIPT>


CREATE TABLE IF NOT EXISTS `registration` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(20) NOT NULL,
`lastname` varchar(20) NOT NULL,

`gender` varchar(20) NOT NULL,


PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

running code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜