假设我有以下代码:
SELECT * FROM table where company LIKE '%Auto%'
我收到更多结果,我希望有一个按字母顺序对结果进行排序的选项,假设用户想要对以“C”开头的搜索结果进行排序!
最诚挚的问候,
答案 0 :(得分:26)
好吧,你似乎在谈论两件不同的事情。如果您对排序感兴趣,则需要使用ORDER BY子句:
SELECT * FROM table ORDER BY name
如果您想按字母“C”开头的项目过滤结果,那么您可能希望使用该字母添加另一个LIKE子句:
SELECT * FROM table where company LIKE '%Auto%' AND name LIKE 'C%'
此外,您会注意到名称过滤器在查询后只有%
。这是“以...开头”
答案 1 :(得分:7)
使用ORDER BY
子句:
SELECT *
FROM table
where company LIKE '%Auto%'
order by company
答案 2 :(得分:1)
添加ORDER BY company
,假设您要按公司价值排序。
答案 3 :(得分:0)
试一试.....这是我的工作实例 排序记录字典顺序ABCD .... Z当我点击一封信然后其显示所有名称以A字母开头,然后点击C信然后它的节目所有名字都以C字母开头
<?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 />
: <?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 ;
正在运行的代码