尝试通过sqlite3订购DD-MM

时间:2018-11-27 20:12:12

标签: sql sqlite

我将生日存储为DD-MM,我没有考虑对它们进行排序,但是现在无论如何我都在尝试对它们进行排序,这可以做到吗?我尝试过:

const bdays = db.prepare('SELECT id, server, substr(datum, 1, 2) || "-" || substr(datum, 4, 5) as date FROM birthdays WHERE server = ? ORDER BY date').all(message.guild.id);
let people = '';
for (const data of bdays) {
    people += `**${client.users.get(data.id).username}** - ${data.datum}\n`;
    message.channel.send(data.datum);
}

但这给了我SqliteError: near "substr": syntax error

1 个答案:

答案 0 :(得分:0)

因为SQLite不支持to_date函数。

您可以尝试使用substr和多个order by

模式(SQLite v3.18)

CREATE TABLE T(
    col VARCHAR(50)
);

INSERT INTO T VALUES ('02-28');
INSERT INTO T VALUES ('03-05');
INSERT INTO T VALUES ('01-01');

查询#1

SELECT  col,substr(col,1,2),substr(col,4,2)
FROM T
ORDER BY substr(col,1,2),substr(col,4,2);

| col   | substr(col,1,2) | substr(col,4,2) |
| ----- | --------------- | --------------- |
| 01-01 | 01              | 01              |
| 02-28 | 02              | 28              |
| 03-05 | 03              | 05              |

View on DB Fiddle