在SQLite中按多个字段排序
发布时间:2020-12-12 19:05:20 所属栏目:百科 来源:网络整理
导读:我有一个简单的表,如:int id,date created_at,date updated_at.我想对行进行排序,以便任何具有updated_at的行将按其排序,而任何不具有的行将按created_at排序.问题是,像: SELECT * FROM table ORDER BY updated_at,created_at 不起作用.我一直在看自定义订
我有一个简单的表,如:int id,date created_at,date updated_at.我想对行进行排序,以便任何具有updated_at的行将按其排序,而任何不具有的行将按created_at排序.问题是,像:
SELECT * FROM table ORDER BY updated_at,created_at 不起作用.我一直在看自定义订单条款,例如: ORDER BY CASE WHERE updated_at ISNULL <do something> END,created_at 但似乎无法得到任何工作. 编辑: id created_at updated_at -- ---------- ---------- 1 2009-01-08 null 2 2009-09-08 null 3 2009-07-02 null 4 2009-09-05 2009-09-06 5 2009-04-01 null 6 2009-09-07 2009-09-08 我想要的结果如下: id created_at updated_at -- ---------- ---------- 6 2009-09-07 2009-09-08 4 2009-09-05 2009-09-06 2 2009-09-08 null 3 2009-07-02 null 5 2009-04-01 null 1 2009-01-08 null看起来像你在寻找: ORDER BY COALESCE(updated_at,created_at) 每the docs,合并回报
编辑:鉴于OP的编辑,他可能想要: ORDER BY COALESCE(updated_at,'1929-9-9') DESC,created_at DESC 或类似的DESC订购技巧;或者可能 ORDER BY MAX(COALESCE(updated_at,'1929-9-9'),created_at) DESC 不可能从单一且非常特殊的例子中确切地说出OP想要什么,但是这些方法中的某些方法的变体应该解决他的实际问题,无论该问题实际上可能在具体,精确和完整的细节中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |