如何更新MySQL选择查询的结果

时间:2017-09-14 15:36:52

标签: mysql sql

我有以下SELECT查询...

func saveBase64StringToPDF(base64String: String, title: String) -> UIDocumentInteractionController? {
    guard var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last,
          let convertedData = Data(base64Encoded: base64String)
        else {
            return nil
        }
    }

    documentsURL.appendPathComponent(title)

    do {
        try convertedData.write(to: documentsURL)
    } catch {
        print("FAILED TO WRITE")
    }

    let interactionController = UIDocumentInteractionController(url: documentsURL)
    return interactionController
}

我正在尝试编辑查询以使其成为UPDATE查询,并设置users.editoverride = 1.我的尝试低于...

SELECT users.id, users.firstname, users.lastname, users.email,
users.live, users.editoverride, 
usertenderstage.tenderId, 
usertenderstage.stageId, usertenderstage.statusId
FROM users
JOIN usertenderstage ON usertenderstage.userId = users.id
WHERE users.live = 1 AND usertenderstage.tenderId = 1 
AND usertenderstage.stageId = 2 AND usertenderstage.statusid = 6

但我收到错误消息

UPDATE users.id, users.firstname, users.lastname, users.email,
users.live, users.editoverride, 
usertenderstage.tenderId, 
usertenderstage.stageId, usertenderstage.statusId
FROM users
JOIN usertenderstage ON usertenderstage.userId = users.id
SET users.editOverride = 1
WHERE users.live = 1 AND usertenderstage.tenderId = 1 
AND usertenderstage.stageId = 2 AND usertenderstage.statusid = 6

有人可以帮助我知道我做错了什么吗?感谢。

1 个答案:

答案 0 :(得分:1)

以下应该工作:

int [] scans = new int[4];
System.out.println("Enter 4 numbers into the array: " );
Scanner scanner = new Scanner(System.in);
for (int i=0; i<scans.length; i++) {
    scans [i] = scanner.nextInt();
}

String prefix = "";
String result = "";
for(int i=0; i<scans.length; i++){
    result = result + prefix + scans[i];
    prefix = "|";
}

System.out.println(result);

似乎问题出在MySQL上,因为你收到错误&#34;#1093 - 你无法指定目标表&#39;用户&#39;用于FROM子句中的更新&#34;。为了解决这个问题,请将其嵌套一层,如下所示:

UPDATE users
SET editoverride = 1
WHERE id IN
    (SELECT users.id
     FROM users
     JOIN contractorscontractsappliedfor ON contractorscontractsappliedfor.contractorId = users.id
     JOIN usertenderstage ON usertenderstage.userId = users.id
     WHERE users.live = 1
       AND contractorscontractsappliedfor.contractid = 1
       AND usertenderstage.stageId = 2
       AND usertenderstage.statusid = 6)

我还没有对其进行测试,因为它不具备MySQL的便利性。