简体   繁体   English

如何限制“ SELECT”结果?

[英]How to limit “SELECT” results?

I have the next code and he showing new categoires im making to the users. 我有下一个代码,他向用户显示了新的类别。 the problem I dont know how to limit the results and make like limit for 5 last categiroes? 问题,我不知道如何限制结果,并限制5个最后的categiroes? thanks! 谢谢! sorry about bad english. 对不起,英语不好。 thanks 谢谢

   <?php
    $stmt = $pdo->prepare('SELECT * FROM categories');
    $stmt->execute();
    echo $stmt->rowCount();
?>
</span>
</a>

<ul class="dropdown-menu extended notification">
    <li>
        <p>Categories</p>
    </li>

<?php
    if($stmt->rowCount() > 0) {
        foreach($stmt->fetchAll() as $row) {
            $html = '<li>';
            $html .= '<div class="alert alert-info clearfix">';
            $html .= '<span class="alert-icon"><i class="fa fa-flag"></i></span>';
            $html .= '<div class="noti-info">';
            $html .= 'Category #'.$row['CategoryID'].' '.$row['CategoryName'].'';
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</li>';

            echo $html;
        }
    } else {
        $html = '<li>';
        $html .= '<div class="alert alert-danger clearfix">';
        $html .= '<span class="alert-icon"><i class="fa fa-flag"></i></span>';
        $html .= '<div class="noti-info">';
        $html .= '<a href="#"> There are no available categories.</a>';
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</li>';

        echo $html;
    }
?>

Use the ORDER BY , DESC and LIMIT on your query, ie: 在查询中使用ORDER BYDESCLIMIT ,即:

SELECT * FROM categories ORDER BY ColumnNameEx DESC LIMIT 5;

ORDER BY

In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing extra sorting. 在某些情况下, MySQL可以使用索引来满足ORDER BY子句,而无需进行额外的排序。
The index can also be used even if the ORDER BY does not match the index exactly, as long as all unused portions of the index and all extra ORDER BY columns are constants in the WHERE clause 即使ORDER BY与索引不完全匹配,也可以使用索引,只要索引的所有未使用部分和所有额外的ORDER BY列在WHERE子句中都是常量

http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html


DESC

The default sort order is ascending, with smallest values first. 默认的排序顺序是升序,最小值先出现。 To sort in reverse (descending) order, add the DESC keyword to the name of the column you are sorting by. 要以相反的顺序(降序)排序,请将DESC关键字添加到排序依据的列名称中。

http://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html http://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html


LIMIT

If you need only a specified number of rows from a result set, use a LIMIT clause in the query, rather than fetching the whole result set and throwing away the extra data. 如果从结果集中只需要指定数量的行,请在查询中使用LIMIT子句,而不是获取整个结果集并丢弃多余的数据。

https://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html https://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html


在查询末尾添加“ LIMIT x”,x是您想要获得的结果数量

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

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