SQL-使用来自另一个表的MAX日期更新字段

时间:2018-06-20 10:14:59

标签: sql sql-server sql-update

我正在使用SQL Server,并希望使用FileInputStream fileInputStream = new FileInputStream(selectedFile); URL url = new URL(SERVER_URL); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);//Allow Inputs connection.setDoOutput(true);//Allow Outputs connection.setUseCaches(false);//Don't use a cached Copy connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("ENCTYPE", "multipart/form-data"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); //creating new dataoutputstream dataOutputStream = new DataOutputStream(connection.getOutputStream()); //writing bytes to data outputstream dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd); dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + selectedFilePath + "\"" + lineEnd); dataOutputStream.writeBytes(lineEnd); //returns no. of bytes present in fileInputStream bytesAvailable = fileInputStream.available(); //selecting the buffer size as minimum of available bytes or 1 MB bufferSize = Math.min(bytesAvailable,maxBufferSize); //setting the buffer as byte array of size of bufferSize buffer = new byte[bufferSize]; //reads bytes from FileInputStream(from 0th index of buffer to buffersize) bytesRead = fileInputStream.read(buffer,0,bufferSize); //loop repeats till bytesRead = -1, i.e., no bytes are left to read while (bytesRead > 0){ //write the bytes read from inputstream dataOutputStream.write(buffer,0,bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable,maxBufferSize); bytesRead = fileInputStream.read(buffer,0,bufferSize); } dataOutputStream.writeBytes(lineEnd); dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); Log.i(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode); //response code of 200 indicates the server status OK if(serverResponseCode == 200){ runOnUiThread(new Runnable() { @Override public void run() { tvFileName.setText("File Upload completed.\n\n You can see the uploaded file here: \n\n" + "http://coderefer.com/extras/uploads/"+ fileName); } }); } //closing the input and output streams fileInputStream.close(); dataOutputStream.flush(); dataOutputStream.close(); 表中保存的该供应商的最新发票日期来更新Suppliers表。

我尝试过:

invoices

但这只是从整个UPDATE Suppliers SET Last_Billed_Date = (SELECT MAX(invoice_date) as 'MAXDATE' FROM Invoices i INNER JOIN Suppliers s ON i.supplierID=s.id ) 表中返回MAX日期。

3 个答案:

答案 0 :(得分:1)

您可以使用相关子查询来做您想做的事情:

UPDATE Suppliers
    SET Last_Billed_Date = (SELECT MAX(i.invoice_date)
                            FROM Invoices i 
                            WHERE i.supplierID = Suppliers.id);

子查询中不需要JOIN

答案 1 :(得分:0)

尝试一下:

UPDATE f1
SET f1.Last_Billed_Date = MAX(f2.invoice_date)
FROM Suppliers f1 INNER JOIN Invoices f2 ON f2.supplierID = f1.id

答案 2 :(得分:0)

UPDATE Suppliers S
SET S.Last_Billed_Date = (SELECT MAX(i.invoice_date)
                        FROM Invoices i 
                        WHERE i.supplierID = S.supplierID );