0

我想知道是否有查询(PHP + MySQL 代码),在我的表中有以下行:

    id name    featured
    ----------------
    1 Robert   0
    2 John     1
    3 Pax      1
    4 Max      0
    5 Emily    0
    6 Sara     0
    7 Estella  0

在我的页面中显示以下内容(具有特色列值的优先级并按 ASC 排序以容纳其余部分):

    id name    featured
    ----------------
    2 John     1
    3 Pax      1
    1 Robert   0
    4 Max      0
    5 Emily    0
    6 Sara     0
    7 Estella  0

这是我的实际代码:

$sql = "SELECT * FROM employees ORDER BY id desc';

我希望有人帮助我,我在谷歌中找不到类似的东西:/

4

3 回答 3

1

您需要先按featured列排序,然后按id

SELECT * FROM employees ORDER BY featured DESC, id ASC

输出:

| id |    name | featured |
|----|---------|----------|
|  2 |    John |        1 |
|  3 |     Pax |        1 |
|  1 |  Robert |        0 |
|  4 |     Max |        0 |
|  5 |   Emily |        0 |
|  6 |    Sara |        0 |
|  7 | Estella |        0 |

看到这个 SQLFiddle

于 2012-12-05T09:03:41.387 回答
1
$sql = "SELECT * FROM employees ORDER BY featured desc';
于 2012-12-05T09:01:11.770 回答
0

尝试这个

 SELECT * FROM employees ORDER BY featured desc, id asc
于 2012-12-05T09:05:21.007 回答