将所需的行ID放在顶部

时间:2016-09-15 14:34:33

标签: php mysql

我有以下PHP代码和查询来从数据库中检索产品:

$sql = "SELECT * FROM products WHERE id_category = :id_category";
$query = $conn->prepare($sql);
$query->execute(array
(
    'id_category' => $id_category
));

while($row = $query->fetch(PDO::FETCH_ASSOC))
{
    extract($row);
    echo $id_product; //1, 2, 3, 4, 5, 6, 7 etc
}

我需要的是将id 5 的产品行放在最上面,我根本就不知道该怎么做。我对ORDER BY所有尝试都失败了。

5 个答案:

答案 0 :(得分:1)

使用UNION of 2查询来实现此目的。让我在这里重写一下查询:

SELECT * FROM products WHERE id_category = :id_category WHERE id=5
UNION
SELECT * FROM products WHERE id_category = :id_category WHERE id!=5

答案 1 :(得分:1)

我会填充一个新数组并使用array_unshift();

$rowHolder = array();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
    if ($row['id_category'] !== 5) {
        $rowHolder[] = $row;
    } else {
        array_unshift($rowHolder, $row);
    }
}

$ rowHolder现在保存所有行,其中行在零索引处具有id = 5.

答案 2 :(得分:1)

您可以使用ORDER BY子句特定返回的订单,以便您可以尝试:

select * from `products` where `id_category` = :id_category order by `id`=5 desc;

作为更改顺序的示例,请考虑以下

mysql> select * from accounts;
+----+-----------+----------+----------+
| id | sponsorID | open     | close    |
+----+-----------+----------+----------+
|  1 |        23 | 6:00 AM  | 10:30 PM |
|  2 |        23 | 6:00 AM  | 8:30 PM  |
|  3 |        21 | 11:00 AM | 5:30 PM  |
|  4 |        23 | 7:00 AM  | 2:30 PM  |
|  5 |        24 | 9:00 AM  | 11:30 PM |
|  6 |        25 | 6:00 AM  | 9:30 PM  |
|  7 |        99 | 6:00 AM  | 9:30 PM  |
+----+-----------+----------+----------+


mysql> select * from accounts order by id=5 desc;
+----+-----------+----------+----------+
| id | sponsorID | open     | close    |
+----+-----------+----------+----------+
|  5 |        24 | 9:00 AM  | 11:30 PM |
|  1 |        23 | 6:00 AM  | 10:30 PM |
|  2 |        23 | 6:00 AM  | 8:30 PM  |
|  3 |        21 | 11:00 AM | 5:30 PM  |
|  4 |        23 | 7:00 AM  | 2:30 PM  |
|  6 |        25 | 6:00 AM  | 9:30 PM  |
|  7 |        99 | 6:00 AM  | 9:30 PM  |
+----+-----------+----------+----------+

您可以在订单中指定多个明确的术语,或者混合后续列的顺序

mysql> select * from accounts order by id=5 desc, id desc;
+----+-----------+----------+----------+
| id | sponsorID | open     | close    |
+----+-----------+----------+----------+
|  5 |        24 | 9:00 AM  | 11:30 PM |
|  7 |        99 | 6:00 AM  | 9:30 PM  |
|  6 |        25 | 6:00 AM  | 9:30 PM  |
|  4 |        23 | 7:00 AM  | 2:30 PM  |
|  3 |        21 | 11:00 AM | 5:30 PM  |
|  2 |        23 | 6:00 AM  | 8:30 PM  |
|  1 |        23 | 6:00 AM  | 10:30 PM |
+----+-----------+----------+----------+

mysql> select * from accounts order by id=5 desc, id=1 desc, id desc;
+----+-----------+----------+----------+
| id | sponsorID | open     | close    |
+----+-----------+----------+----------+
|  5 |        24 | 9:00 AM  | 11:30 PM |
|  1 |        23 | 6:00 AM  | 10:30 PM |
|  7 |        99 | 6:00 AM  | 9:30 PM  |
|  6 |        25 | 6:00 AM  | 9:30 PM  |
|  4 |        23 | 7:00 AM  | 2:30 PM  |
|  3 |        21 | 11:00 AM | 5:30 PM  |
|  2 |        23 | 6:00 AM  | 8:30 PM  |
+----+-----------+----------+----------+

答案 3 :(得分:1)

在MySQL中它是微不足道的:

SELECT ...
...
ORDER BY (id_category = 5) DESC, id_category

这可以通过获取(id_category = 5)的结果,它是一个布尔值true / false,并将其转换为整数0 / 1并按DESC顺序对它们进行排序。这会将所有5条记录放在结果集的TOP中,然后其余的非5条记录在第二位id_category子句中照常排序。

答案 4 :(得分:0)

试试这个:

$sql = "SELECT *,(id_category=5) as top FROM products WHERE id_category = :id_category ORDER BY top DESC";
$query = $conn->prepare($sql);
$query->execute(array
(
    'id_category' => $id_category
));