В SQLite.swift подзапрос не работает из-за отсутствия parsentesis ().
let users = Table(kUsersTableName)
let users_types = Table(kUsersTypesTableName)
let agendas = Table(kAgendasTableName)
let agendas_query = agendas
.select(agendas[agenda_date])
.filter(users[user_id] == agendas[user_id])
.filter(agendas[agenda_date] < _timestamp)
.order(agendas[agenda_date].desc)
.limit(1)
let query = users
.select(agendas_query, users[*])
.join(.leftOuter, users_types, on: users[type_id] == users_types[type_id])
.order(users[surname].desc, users[name].desc)
Неверный вывод SQL Запрос:
"SELECT SELECT "agendas"."agenda_date" FROM "agendas" WHERE (("users"."user_id" = "agendas"."user_id") AND ("agendas"."agenda_date" < ?)) ORDER BY "agendas"."agenda_date" DESC LIMIT 1, "users".* FROM "users" LEFT OUTER JOIN "users_types" ON ("users"."type_id" = "users_types"."type_id") ORDER BY "users"."surname" DESC, "users"."name" DESC"
Запрос должен быть:
"SELECT (SELECT "agendas"."agenda_date" FROM "agendas" WHERE (("users"."user_id" = "agendas"."user_id") AND ("agendas"."agenda_date" < ?)) ORDER BY "agendas"."agenda_date" DESC LIMIT 1), "users".* FROM "users" LEFT OUTER JOIN "users_types" ON ("users"."type_id" = "users_types"."type_id") ORDER BY "users"."surname" DESC, "users"."name" DESC"