我需要按日期/时间字段对PostgreSQL表进行排序,例如last_updated.
但是,该字段允许为空或空,我想与空记录last_updated来之前,非空last_updated.
这可能吗?
order by last_updated asc /* and null last_updated records first ?? */
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有 Products 表,其中包含各种类型的产品。在每条记录中都有列调用updateDetails,它是一个JSON具有以下属性的对象。
const updateDetails = { time: '2020-05-28T05:53:31.540Z', userId: 1, officeId: 2}
Run Code Online (Sandbox Code Playgroud)
但在产品表中的一些记录updateDetails时null,将任何用户编辑的记录后更新。
我的示例数据库记录如下。
[
{
name: 'Product 1',
type: 'a',
updateDetails: { time: '2020-05-28T05:53:31.540Z', userId: 1, officeId: 2 }
},
{
name: 'Product 2',
type: 'a',
updateDetails: null
},
{
name: 'Product 3',
type: 'a',
updateDetails: { time: '2020-05-27T05:53:31.540Z', userId: 1, officeId: 2 }
},
{
name: 'Product 4',
type: 'a',
updateDetails: null
},
{
name: 'Product 5',
type: 'a', …Run Code Online (Sandbox Code Playgroud) 我使用JPA标准API从日期库中获取记录.我有实体记录,字段dateTime可以为null.我会编码:
public List<Record> find(RecordFilter recordFilter, int page, int pageSize) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Record> criteriaQuery = criteriaBuilder.createQuery(Record.class);
Root<Record> recordRoot = criteriaQuery.from(Record.class);
/*
* JOINS. Left Joins are used for optional fields, or fields inside of the optional fields.
*/
Join<Record, Agency> recordAgencyJoin = recordRoot.join(RecordTable.FIELD_AGENCY);
//Some other joins
//This is where I had the problem.
applyOrderBy(criteriaQuery, criteriaBuilder, recordRoot);
/*
* Specify which columns to select and their order.
* criteriaQuery.multiselect(....);
*/
applyMultiSelect(recordRoot, recordAgencyJoin, /*other joins*/ criteriaQuery); …Run Code Online (Sandbox Code Playgroud) postgresql ×2
criteria-api ×1
express ×1
java ×1
javascript ×1
jpa ×1
node.js ×1
null ×1
sequelize.js ×1
sql ×1
sql-order-by ×1