订购所选数据

时间:2016-01-16 17:25:38

标签: sql sql-order-by

我正在制作一个图书馆项目。我想展示按此顺序退还书籍的人:未退还书籍15天的人,未退还书籍但仍有15天时限的人员和人员谁归还了他们的书。该表如下所示:

id | pupil  | professor | date_of_taking      | expected_date       | returned | book |
---------------------------------------------------------------------------------------
1  |1       |1          | 2016-01-16 00:00:00 | 2016-01-30 00:00:00 | 0        | 20   |
2  |2       |1          | 2016-01-07 00:00:00 | 2016-01-14 00:00:00 | 0        | 93   |
3  |1       |1          | 2016-01-09 00:00:00 | 2016-01-30 00:00:00 | 1        | 14   |

瞳孔栏包含拿书的人的身份证。教授专栏包含了给这本书的人的身份。 book列包含所购书籍的id。所有这些数据都在另一个表中。服用日期是指该人取书的日期,预计日期是该人应该将其退还的日期。

所以,如果您不理解之前的解释,我真正想要的是: 首先显示身份2,因为时间限制已到期,他应该返回该书。之后,显示1,因为他仍然没有退回书,但还有更多的时间。最后,显示3,因为他已经退回了这本书。现在想象一下,这个表中有数千行。如何命令它按照我在这里解释的顺序显示数据。

这就是我所拥有的。我只需要帮助订购数据。

SELECT 
    A.date_of_taking,
    A.expected_at,
    A.returned,
    B.name as pupil,
    C.name as prof,
    D.author,
    D.title
FROM taken AS A
INNER JOIN pupils AS B ON A.pupil = B.id
INNER JOIN users AS C ON A.professor = C.id
INNER JOIN books AS D ON A.book = D.id
LIMIT 50

顺便说一下,表格中两个日期之间的差异不是严格的15天,因为这只是一个测试表。
这也是PHP中的代码,但我想将这3个查询合并为一个。如果可能的话。

$stmt = $this->db()->query("SELECT 
        A.date_of_taking,
        A.expected_at,
        A.returned,
        B.name as pupil,
        C.name as prof,
        D.autor,
        D.title

        FROM taken AS A
        INNER JOIN pupils AS B ON A.pupil = B.id
        INNER JOIN users AS C ON A.professor = C.id
        INNER JOIN books AS D ON A.book = D.id
        WHERE A.returned='0' AND A.date_of_taking < A.expected_at ORDER BY A.date_of_taking ASC");

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $this->db()->query("SELECT 
        A.date_of_taking,
        A.expected_at,
        A.returned,
        B.name as pupil,
        C.name as prof,
        D.autor,
        D.title

        FROM taken AS A
        INNER JOIN pupils AS B ON A.pupil = B.id
        INNER JOIN users AS C ON A.professor = C.id
        INNER JOIN books AS D ON A.book = D.id
        WHERE A.date_of_taking > A.expected_at ORDER BY A.date_of_taking ASC");

$var = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($var as $value) {
    $result[] = $value;
}
$stmt = $this->db()->query("SELECT 
        A.date_of_taking,
        A.expected_at,
        A.returned,
        B.name as pupil,
        C.name as prof,
        D.autor,
        D.title

        FROM taken AS A
        INNER JOIN pupils AS B ON A.pupil = B.id
        INNER JOIN users AS C ON A.professor = C.id
        INNER JOIN books AS D ON A.book = D.id
        WHERE A.returned='1' ORDER BY A.date_of_taking ASC");

$var = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($var as $value) {
    $result[] = $value;
}
return $result;

2 个答案:

答案 0 :(得分:1)

如果指定

ORDER BY RETURNED, EXPECTED_DATE

然后返回= 0之前返回= 0 并且在该命令中,future_date将来会更多。

答案 1 :(得分:0)

我错过了什么或者你需要的只是:

SELECT 
    A.date_of_taking,
    A.expected_at,
    A.returned,
    B.name as pupil,
    C.name as prof,
    D.author,
    D.title
FROM taken AS A
INNER JOIN pupils AS B ON A.pupil = B.id
INNER JOIN users AS C ON A.professor = C.id
INNER JOIN books AS D ON A.book = D.id
ORDER BY ( CURRENT_DATE -  A.date_of_taking ) DESC  
LIMIT 50

注1 不同的数据库可能有不同的日期算术规则。因此,您可能需要更改(DATE2 - DATE1)另一个依赖于数据库的函数,返回两个日期之间的天数(例如DATEDIFF())

Note2 多个数据库使用CURRENT_DATE返回...当前日期。在其他情况下,这可能是SYSDATE或其他。