PHP MySQL排序顺序ASC / DESC仅用于显示的记录

时间:2014-10-07 01:34:12

标签: php mysql sorting

我有一个返回接近1000条记录的查询。使用分页,我每页显示100条记录。太棒了...没问题。我也可以按降序升序按姓氏或名字排序。好到目前为止。第一页返回以A到C开头的姓氏记录。我遇到的问题是,当我点击姓氏下降时,我会得到姓氏以Z开头的记录。我查询结尾的记录,我想要的获得从C到A的结果(我的第一页显示的内容......在每个页面中重复相同的功能。

这是我得到的......

$orderColumn        =       'lastName';
$orderDirection     =       'ASC';

if( isset($_POST["oc"]) && $_POST["oc"] !== '' ) {  $orderColumn        = $_POST["oc"]; }
if( isset($_POST["od"]) && $_POST["od"] !== '' ) {  $orderDirection     = $_POST["od"]; }

$per_page = 100;

$query = "SELECT  *  FROM table as t
            LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
            LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
            WHERE t3.fk_utID = 7 and t.interviewed = 0";

$result = $db->query($query);
$count    =     mysql_num_rows($result);

$total = ceil($count/$per_page);

if ($_GET['page']) {

    $page = $_GET['page'];

}


$offset = (($page-1)*$per_page);

$query2 = "SELECT firstName as first, lastName as last FROM table
                LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
                LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
                WHERE t3.fk_utID = 7 and interviewed = 0 order by $orderColumn $orderDirection LIMIT $offset, $per_page";

$res = $db-> query($query2);

while($row = mysql_fetch_array($res)){ 

echo "<span style='display: inline-block; width: 15%;'>$row[first]</span>";
echo "<span style='display: inline-block; width: 15%;'>$row[last]</span>";

}

1 个答案:

答案 0 :(得分:0)

我在评论中说的是什么..顺便说一句,我在手机上,所以这可能是未格式化的,或者需要一段时间......

Select what_you_need
From
(   select your_inner_select
    From table t
    LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
    LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
    WHERE t3.fk_utID = 7 and interviewed = 0 LIMIT $offset, $per_page
    ORDER BY $orderColumn ASC
)t 
order by $orderColumn $orderDirection