我有两张桌子,加入他们,现在我只想数其中一张
Select(count(myOtherTable.name), from: myTable).join(myOtherTable).on(myTable.name == myOtherTable.someName)
执行该查询我得到
操作无法完成。(SwiftKuery:QueryError 2.)
一些想法我的错误是什么?
我怀疑他的问题是你应该使用count(myOtherTable.someName)
.
下面是示例代码,演示了您的问题中的查询根据从它派生的数据库模式按预期运行。
import Foundation
import Dispatch
import SwiftKueryMySQL
import SwiftKuery
let semaphore = DispatchSemaphore(value: 0)
func handleError(_ error: String) {
print(error)
semaphore.signal()
}
class Person : Table {
let tableName = "infos"
let name = Column("name", String.self)
}
class Customer : Table {
let tableName = "customers"
let name = Column("name", String.self)
let nickname = Column("nickname", String.self)
}
let mySQLPool = MySQLConnection.createPool(user: "root", database: "playground", poolOptions: ConnectionPoolOptions(initialCapacity: 1, maxCapacity: 10))
let infos = Person()
let customers = Customer()
let insertInfo = Insert(into: infos, rows: [["personone"],["persontwo"],["personthree"]])
let insertCustomer = Insert(into: customers, rows: [["p2","persontwo"]])
let select = Select(count(customers.name), from: infos).join(customers).on(infos.name == customers.nickname)
mySQLPool.getConnection() { connection, error in
guard let connection = connection else {
guard let error = error else {
return handleError("Unable to get connection: Unknown error")
}
return handleError("Unable to get connection: \(error.localizedDescription)")
}
infos.create(connection: connection) { result in
guard result.success else {
guard let error = result.asError else {
return handleError("Unable to create table: Unknown error")
}
return handleError("Unable to create table: \(error.localizedDescription)")
}
customers.create(connection: connection) { result in
guard result.success else {
guard let error = result.asError else {
return handleError("Unable to create second table: Unknown error")
}
return handleError("Unable to create table: \(error.localizedDescription)")
}
connection.execute(query: insertInfo) { result in
guard result.success else {
guard let error = result.asError else {
return handleError("Unable to insert info: Unknown error")
}
return handleError("Unable to insert info: \(error.localizedDescription)")
}
connection.execute(query: insertCustomer) { result in
guard result.success else {
guard let error = result.asError else {
return handleError("Unable to insert customer: Unknown error")
}
return handleError("Unable to insert customer: \(error.localizedDescription)")
}
connection.execute(query: select) { result in
guard result.success else {
guard let error = result.asError else {
return handleError("Unable to create table: Unknown error")
}
return handleError("Unable to create table: \(error.localizedDescription)")
}
result.asRows() { rows, error in
guard let rows = rows else {
guard let error = error else {
return handleError("Unable to get rows: Unknown error")
}
return handleError("Unable to get rows: \(error.localizedDescription)")
}
for row in rows {
print("row: \(row)")
}
semaphore.signal()
}
}
}
}
}
}
}
semaphore.wait()